1

I am using firebase for hosting cloud functions, since many functions (about every) I need to make the http request and get the json body to get the data from it. However, the callback doesn't work quite well for me, I've searched some existing answers but still get stuck on this. Here is the code snippet, options are declared before and if I do not put the request within get_request_handler it works fine.:

function get_request_handler(assistant, input_url, callback) {
  req(options, function (error, response, body) {
     if (!error && response.statusCode == 200) {
      var cookie = req.cookie('BPMSTS=' + body );
      var headers = {
          'Content-Type': 'application/json',
          'Cookie': cookie
      };
      var option = {
          url: input_url,
          method: 'GET',
          headers: headers
      }
      req(option, function(error, res, body) {
          assistant.ask(input_url);
          if (!error && res.statusCode == 200) {
              callback(JSON.parse(body));
          } else {
              assistant.ask('inner request with error code: ' + (res.statusCode).toString());
          }
      });
     } else {
      assistant.ask('outer request with error code: ' + (response.statusCode).toString());
    }
  });
}

I call the function as follows:

get_request_handler(assistant, workflow_url, function(cur_json){assistant.ask(cur_json);});

The problem right now is the first request can't be made in the get_request_handler function. In other words, it only goes in to get_request_handler but not go into that request body. If I do not create get_request_handler and left req(options, function (error, response, body) { ... } it works without any problem. Any ideas on this?

Note: I just checked firebase log and it says for this line: req(options, function (error, response, body) it got TypeError: Assignment to constant variable. at get_request_handler (/user_code/index.js:116:13)

1 Answers1

2

You have a lot of problems here, but the basic one is that you're trying to call assistant.ask() more than once in your response to the user. Once you call assistant.ask(), no further responses can be sent, and none of the other calls to ask() will be handled.

It looks like you're using it for debugging. That seems a really poor choice when you should be using console.log().

You also indicated that you're using Firebase Functions. Note that calls out from a Firebase function are restricted if you're on the free plan. If you're on one of the paid plans there is no restriction, and there is a free tier which should be more than sufficient for testing.

Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • Hi Prisoner. Yea I am in the paid plan so that I can make external request without putting the req in the get_request_handler. Well I didn't call assistant.ask() twice, when I call it the other one will be commented out. I guess the only issue here would be why it can't go in the request body if I put it within a function? – Will Yuling Liu Aug 16 '17 at 16:48
  • Then I'm not sure I understand the problem, nor what you mean by "why it can't go in the request body if I put it within a function". Please update your original question with an indication of what is and isn't getting called or what you can and can't access. – Prisoner Aug 16 '17 at 19:16
  • Edited. If there is still anything unclear lemme know! Thanks in advance. – Will Yuling Liu Aug 16 '17 at 20:22