简体   繁体   English

通过api.ai的函数回调获取json正文

[英]Get json body through function callback for api.ai

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. 我正在使用Firebase托管云功能,因为许多功能(大约每个功能)都需要发出http请求并获取json主体以从中获取数据。 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.: 这是代码段,选项是在之前声明的,如果我不将请求放入get_request_handler中,它将正常工作。

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. 现在的问题是无法在get_request_handler函数中发出第一个请求。 In other words, it only goes in to get_request_handler but not go into that request body. 换句话说,它仅进入get_request_handler而不进入该请求正文。 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? 如果我不创建get_request_handler并保留req(options,function(error,response,body){...},那么它没有任何问题。对此有什么想法吗?

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) 注意:我刚刚检查了Firebase日志,它说了这一行:req(选项,函数(错误,响应,正文),它得到TypeError:分配给常量变量,位于get_request_handler(/user_code/index.js:116:13)

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. 您在这里遇到了很多问题,但是最基本的问题是您试图多次响应用户响应来调用assistant.ask() Once you call assistant.ask() , no further responses can be sent, and none of the other calls to ask() will be handled. 调用assistant.ask() ,将无法再发送任何响应,也不会处理对ask()的其他调用。

It looks like you're using it for debugging. 您似乎正在使用它进行调试。 That seems a really poor choice when you should be using console.log() . 当您应该使用console.log()时,这似乎是一个糟糕的选择。

You also indicated that you're using Firebase Functions. 您还表示您正在使用Firebase Functions。 Note that calls out from a Firebase function are restricted if you're on the free plan. 请注意,如果您使用的是免费方案,则从Firebase函数中调出电话将受到限制。 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. 如果您使用的是付费计划中的一种,则没有任何限制,并且有免费的套餐,对于测试而言,该套餐应绰绰有余。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM