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)