Google App脚本-函数返回未定义

时间:2018-07-27 23:12:04

标签: javascript google-apps-script slack-api

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

获取频道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对象进行分页时,这似乎可行,但是当对结果进行分页时,我似乎变得不确定。

有什么想法为什么返回的结果是不确定的,即使它可以在函数中使用?

1 个答案:

答案 0 :(得分:2)

我认为在您的递归函数中,不会返回该值。那修改呢?

发件人:

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分别是undefinedokok