gpt4 book ai didi

javascript - 类型错误 : Cannot read property 'then' of undefined Dialogflow Promise

转载 作者:行者123 更新时间:2023-11-30 14:12:39 25 4
gpt4 key购买 nike

我正在使用 Dialogflow 内联编辑器,我正在尝试从外部 API 获取结果,但我在 Firebase 日志中收到以下错误:

TypeError: Cannot read property 'then' of undefined at video (/user_code/index.js:48:9) at WebhookClient.handleRequest (/user_code/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:303:44) at exports.dialogflowFirebaseFulfillment.functions.https.onRequest (/user_code/index.js:78:9) at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9) at /var/tmp/worker/worker.js:725:7 at /var/tmp/worker/worker.js:708:11 at _combinedTickCallback (internal/process/next_tick.js:73:7) at 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"

感谢您的帮助。

最佳答案

return request.get(url)

Request 已定义为请求对象,在本例中它将尝试获取请求属性。

如果你想获取一些东西,你必须使用其他东西,比如请求 promise 。 https://www.npmjs.com/package/request-promise

关于javascript - 类型错误 : Cannot read property 'then' of undefined Dialogflow Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54131941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com