TypeError:无法读取未定义的Dialogflow Promise的属性'then'

时间:2019-01-10 15:29:11

标签: javascript node.js dialogflow actions-on-google

我正在使用Dialogflow内联编辑器,并且试图从外部API获取结果,但是在Firebase日志中却出现以下错误:

  

TypeError:无法读取未定义的属性“ then”       在视频上(/user_code/index.js:48:9)       在WebhookClient.handleRequest(/user_code/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:303:44)       在export.dialogflowFirebaseFulfillment.functions.https.onRequest(/user_code/index.js:78:9)       在cloudFunction(/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)       在/var/tmp/worker/worker.js:725:7       在/var/tmp/worker/worker.js:708:11       在_combinedTickCallback(内部/进程/next_tick.js:73:7)       在process._tickDomainCallback(internal / process / next_tick.js:128:9)

这是我的代码:

'use strict';

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

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 welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }

  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
}

  function video(agent) {
    agent.add(`Sure, You can access to external API`);
    const url = "https://reqres.in/api/users?page=2";
    return request.get(url)
        .then(jsonBody => {
            var body = JSON.parse(jsonBody);
            agent.add(body.data[0].first_name)
            return Promise.resolve(agent);
        })
        .catch(err => {
            console.error('Problem making network call', err);
            agent.add('Unable to get result');
            return Promise.resolve(agent);
        });
    }

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('video', video);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);
});

编辑

我在 index.js 中添加了:

`const request = require('request-promise-native');`

package.json 中的

"request-promise-native": "^1.0.5",
"request": "^2.88"

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

return request.get(url)

请求已被定义为请求对象,在这种情况下,它将尝试获取请求属性。

如果要获取某些内容,则必须使用诸如请求承诺之类的其他内容。 https://www.npmjs.com/package/request-promise

答案 1 :(得分:1)

解决方案是

添加 index.js

`const rp = require('request-promise-native');`

package.json 中的

"request-promise-native": "^1.0.5",
"request": "^2.88"

并将请求更改为 rp 。如下:

return rp.get(url)