簡體   English   中英

TypeError:無法讀取未定義的Dialogflow Promise的屬性'then'

[英]TypeError: Cannot read property 'then' of undefined Dialogflow Promise

我正在使用Dialogflow內聯編輯器,並且試圖從外部API獲取結果,但是在Firebase日志中卻遇到以下錯誤:

TypeError:無法讀取WebhookClient.handleRequest(/user_code/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:303:44)上視頻(/user_code/index.js:48:9)的未定義屬性'then'在/ var /處的cloudFunction(/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)處的export.dialogflowFirebaseFulfillment.functions.https.onRequest(/user_code/index.js:78:9)處在process._tickDomainCallback(內部/ 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"

謝謝你的幫助。

return request.get(url)

已將請求定義為請求對象,在這種情況下,它將嘗試獲取請求屬性。

如果要獲取某些內容,則必須使用其他類似請求承諾的內容。 https://www.npmjs.com/package/request-promise

解決方案是

添加index.js

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

並在package.json中

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

並將請求更改為rp 如下:

return rp.get(url)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM