Javascript 由于同步错误,字符串响应为空

Javascript 由于同步错误,字符串响应为空,javascript,node.js,callback,promise,dialogflow-es,Javascript,Node.js,Callback,Promise,Dialogflow Es,我启动了一个简单的代理,并在其中添加了一个名为“myIntent”的基本意图。我通过Firebase内联编辑器启用了实现,并在指令之后向提供的基本函数添加了一个名为“myFunction”的函数。此函数包含对的HTTPS GET请求示例。 我的目标是根据对外部API的GET调用提供的响应,为我的意图添加一个响应 剧本 意图已正确触发,但我的问题是代理响应始终是空字符串。 我对http调用和函数回调没有太多经验,但我认为问题在于线路 agent.add(info); 在我的脚本结束时,在ht

我启动了一个简单的代理,并在其中添加了一个名为“myIntent”的基本意图。我通过Firebase内联编辑器启用了实现,并在指令之后向提供的基本函数添加了一个名为“myFunction”的函数。此函数包含对的HTTPS GET请求示例。
我的目标是根据对外部API的GET调用提供的响应,为我的意图添加一个响应

剧本 意图已正确触发,但我的问题是代理响应始终是空字符串。

我对http调用和函数回调没有太多经验,但我认为问题在于线路

 agent.add(info);
在我的脚本结束时,在http调用结束之前执行脚本。
我尝试在下面的
myFunction
中插入承诺,但所有这些都只包含错误消息
我理解这个问题,但我无法以任何方式解决它

附言:

我使用诺言的尝试失败了 您仍然可以立即使用
info
,而不是通过异步回调执行操作—现在正在解析承诺。将
agent.add()
调用或
resolve()
调用放入
end
处理程序中,使其仅在数据完成后发生:

var p = new Promise((resolve,reject) => {
    var options = {
        host: `reqres.in`,
        path: `/api/users/2`,
        method: 'GET',
        headers: {'Accept':'application/json'}
    };

    var req = https.request(options, (res) => {
        console.log('statusCode:', res.statusCode);
        console.log('headers:', res.headers);
        let data='';

        res.on('data', (d) => {
            data+= d;
        });

        res.on('end', () => {
            console.log(data);
            let jsonObject = JSON.parse(data);
            resolve(jsonObject.id);
//          ^^^^^^^^^^^^^^^^^^^^^^^
        });
    });

    req.on('error', (e) => {
        reject(e); // don't forget this
    });

    req.write('End of request');
    req.end();
});

p.then(info => {
//     ^^^^ the fulfillment value that you passed to resolve()
    agent.add(info);
}, err => {
    console.log('ERROR');
    console.error(err);
});

请修复第一个代码段的缩进,好吗?对不起,这不起作用。现在控制台中有两个错误:“未处理的拒绝”和“进程中p.then.info(/user\u code/index.js:63:11)处的WebhookClient.add(/user\u code/node\u modules/dialogflow fulfillment/src/dialogflow fulfillment.js:225:13)处的未知响应类型。”@HJuls2我不知道您使用的是什么
对话框流实现
模块,也不知道是什么导致了其中的“未知响应类型”错误,我的回答只解决了您将
信息
传递给
代理的问题。添加
确定,我测试了在控制台中打印值,而不是使用WebhookClient.add,我发现这个承诺是正确的。问题只出现在Dialogflow实现上function@S.Dwyer我的工作解决方案基本上是Bergi的,但我将其封装在一个返回承诺的函数中,即返回承诺的
函数
'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const https=require('https');

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

 
 function myFunction(agent){
    var info='';
    var p=new Promise((resolve,reject)=>{
      
        var options = {
        host: `reqres.in`,
        path: `/api/users/2`,
        method: 'GET',
        headers: {'Accept':'application/json'}
        };
    
        var req = https.request(options, (res) => {
            console.log('statusCode:', res.statusCode);
            console.log('headers:', res.headers);
            let data='';
    
        res.on('data', (d) => {
            data+= d;
        });
    
        res.on('end', () => {
            console.log(data);
            let jsonObject= JSON.parse(data);
            info=jsonObject.id;
        
             console.log(JSON.stringify(info));
            });
        });

        req.on('error', (e) => {
            console.log('ERROR');
            console.error(e);
        });

        req.write('End of request');
        req.end();
        resolve(info);
    });
    
    p.then((agent)=>{
        agent.add(info);
    });

    }
let intentMap = new Map();
  intentMap.set('myIntent',myFunction);
  agent.handleRequest(intentMap);
});
var p = new Promise((resolve,reject) => {
    var options = {
        host: `reqres.in`,
        path: `/api/users/2`,
        method: 'GET',
        headers: {'Accept':'application/json'}
    };

    var req = https.request(options, (res) => {
        console.log('statusCode:', res.statusCode);
        console.log('headers:', res.headers);
        let data='';

        res.on('data', (d) => {
            data+= d;
        });

        res.on('end', () => {
            console.log(data);
            let jsonObject = JSON.parse(data);
            resolve(jsonObject.id);
//          ^^^^^^^^^^^^^^^^^^^^^^^
        });
    });

    req.on('error', (e) => {
        reject(e); // don't forget this
    });

    req.write('End of request');
    req.end();
});

p.then(info => {
//     ^^^^ the fulfillment value that you passed to resolve()
    agent.add(info);
}, err => {
    console.log('ERROR');
    console.error(err);
});