Dan
Dan

Reputation: 3

Google App Script - Function Returning Undefined

I'm trying to call a function that gets a channel ID when given a Slack Workspace and channel name. I can get the correct result within the function, but when I try to call the function elsewhere, it is returning undefined.

Function to get the 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';
      }
}
`

I can clearly see the 'FOUND CHANNEL ID: CXXXXXX' string in the logger, so I'm sure it finds it properly.

But when I call this getChannelID from the main function, it is returning undefined.

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

The weird thing is this seems to work when the JSON object from Slack isn't paginated, but when the results are returned paginated, I just seem to get undefined.

Any ideas why the result it's returning is undefined, even though it works in the function?

Upvotes: 0

Views: 2062

Answers (1)

Tanaike
Tanaike

Reputation: 201378

I think that in your recursive function, the value is not returned. So how about this modification?

From :

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';
}

To :

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';
}

Note :

  • When channelListData.response_metadata.next_cursor && channelListData.response_metadata.next_cursor != "" is true, no value is returned. So I added return.

If this didn't work yet, please tell me. I would like to modify it.

Added :

In my understanding, when the recursive function is run, the process returns to the line which was run. In order to confirm this, I prepared 3 sample functions.

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

When these functions is run by as follows,

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

res1, res2 and res3 are undefined, ok and ok, respectively.

Upvotes: 2

Related Questions