gpt4 book ai didi

javascript - Google 应用程序脚本 - 函数返回未定义

转载 作者:行者123 更新时间:2023-12-01 01:50:45 25 4
gpt4 key购买 nike

我正在尝试调用一个函数,该函数在给定 Slack 工作区和 channel 名称时获取 channel ID。我可以在函数内获得正确的结果,但是当我尝试在其他地方调用该函数时,它返回未定义。

获取 channel ID的函数`

//GET CHANNEL ID FROM LIST OF ALL CHANNELS IN WORKSPACE
function getChannelID(workspaceName, pageLimit, channelName, nextCursor){
var channelListResponseURL = 'https://slack.com/api/conversations.list';
var payload = {
'limit': pageLimit,
'types': 'public_channel, private_channel',
'cursor' : nextCursor
};
var options = createURLargs(workspaceName, payload);
var channelListResponse = UrlFetchApp.fetch(channelListResponseURL, options);
var channelListJson = channelListResponse.getContentText();
var channelListData = JSON.parse(channelListJson);

//iterate through each channel in the returned JSON object and sets the channel ID for the one matching the channelName
for (var i in channelListData.channels){
if(channelListData.channels[i].name == channelName){
var channelID = channelListData.channels[i].id;
Logger.log('FOUND CHANNEL ID: '+ channelID);
return channelID;// IF CHANNEL ID FOUND, THEN EXIT getChannelID FUNCTION AND RETURN CHANNEL ID
}
}
// IF NO CHANNEL ID IS FOUND, THEN CHECK TO SEE IF PAGINATION IS IN EFFECT, UPDATE CURSOR, AND RERUN getChannelID FUNCTION
if (channelListData.response_metadata.next_cursor && channelListData.response_metadata.next_cursor != ""){
nextCursor = channelListData.response_metadata.next_cursor;
getChannelID(workspaceName, pageLimit, channelName, nextCursor);
} else {
// IF CHANNEL PAGINATION IS NOT IN EFFECT, OR REACHED LAST PAGE AND NO RESULT IS FOUND
return 'No Channel Found in Workspace';
}
}
`

我可以在记录器中清楚地看到“FOUND CHANNEL ID: CXXXXXX”字符串,因此我确信它已正确找到它。

但是当我从主函数调用 getChannelID 时,它返回未定义。

  var channelID = getChannelID(workspaceName, pagLimit, channelName, nextCursor);
Logger.log(channelID);

奇怪的是,当 Slack 中的 JSON 对象未分页时,这似乎可以工作,但是当分页返回结果时,我似乎没有定义。

有什么想法为什么它返回的结果是未定义的,即使它在函数中工作?

最佳答案

我认为在你的递归函数中,没有返回值。那么这个修改怎么样?

来自:

if (channelListData.response_metadata.next_cursor && channelListData.response_metadata.next_cursor != ""){
nextCursor = channelListData.response_metadata.next_cursor;
getChannelID(workspaceName, pageLimit, channelName, nextCursor);
} else {
// IF CHANNEL PAGINATION IS NOT IN EFFECT, OR REACHED LAST PAGE AND NO RESULT IS FOUND
return 'No Channel Found in Workspace';
}

致:

if (channelListData.response_metadata.next_cursor && channelListData.response_metadata.next_cursor != ""){
nextCursor = channelListData.response_metadata.next_cursor;
return getChannelID(workspaceName, pageLimit, channelName, nextCursor); // Modified
} else {
// IF CHANNEL PAGINATION IS NOT IN EFFECT, OR REACHED LAST PAGE AND NO RESULT IS FOUND
return 'No Channel Found in Workspace';
}

注意:

  • channelListData.response_metadata.next_cursor && channelListData.response_metadata.next_cursor != "" 为 true 时,不返回任何值。所以我添加了return

如果这还不起作用,请告诉我。我想修改一下。

添加:

根据我的理解,当递归函数运行时,进程返回到运行的行。为了证实这一点,我准备了 3 个示例函数。

功能1
function foo1(value) {
if (value == "") {
foo1("bar");
} else {
return "ok";
}
}
功能2
function foo2(value) {
if (value == "") {
return foo2("bar");
} else {
return "ok";
}
}
功能3
function foo3(value) {
if (value == "") {
foo3("bar");
}
return "ok";
}

当这些函数按如下方式运行时,

var res1 = foo1("");
var res2 = foo2("");
var res3 = foo3("");

res1res2res3 分别为 undefinedok好的,分别。

关于javascript - Google 应用程序脚本 - 函数返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51566595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com