nodeJS 请求方法不起作用,我是现收现付计划
nodeJS request method not working, I am Pay as you go Plan
我正试图从 firebase 云功能中撞上铁路 API,它给我以下错误
我听说请求方法在免费计划中不起作用,但即使我升级为随用随付计划,它仍然无法使用
// 参见 https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// 用于 Dialogflow 实现库文档、示例和报告问题
'use strict';
var request = require('request');
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
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 fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function welcome(agent) {
var options = {
url: 'http://api.railwayapi.com/v2/live/train/' + '17239' + '/date/' + '27-12-2018' + '/apikey/' + 'myjwjpywq4'
}
request(options, function (err, resp, body) {
console.log(body);
})
agent.add(`This message is from Dialogflow's Cloud Functions for Firebase editor!`);
agent.add(new Suggestion(`Quick Reply`));
agent.add(new Suggestion(`Suggestion`));
agent.setContext({ name: 'weather', lifespan: 2, parameters: { city: 'Rome' }});
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
agent.handleRequest(intentMap);
});
错误:
TypeError: request is not a function
at welcome (/user_code/index.js:33: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:71: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)
您对 request
有两个定义。这是第一个:
var request = require('request');
这是第二个,它在函数体内屏蔽了第一个:
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
当您在函数体内调用 request() 时,JavaScript 认为您指的是最里面的 request
,这是传递给它的第一个参数。这显然不是您想要的。那时,request
不是函数。
只要给它们起不同的名字,这样它们就不会相互冲突。
我正试图从 firebase 云功能中撞上铁路 API,它给我以下错误
我听说请求方法在免费计划中不起作用,但即使我升级为随用随付计划,它仍然无法使用
// 参见 https://github.com/dialogflow/dialogflow-fulfillment-nodejs // 用于 Dialogflow 实现库文档、示例和报告问题
'use strict';
var request = require('request');
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
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 fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function welcome(agent) {
var options = {
url: 'http://api.railwayapi.com/v2/live/train/' + '17239' + '/date/' + '27-12-2018' + '/apikey/' + 'myjwjpywq4'
}
request(options, function (err, resp, body) {
console.log(body);
})
agent.add(`This message is from Dialogflow's Cloud Functions for Firebase editor!`);
agent.add(new Suggestion(`Quick Reply`));
agent.add(new Suggestion(`Suggestion`));
agent.setContext({ name: 'weather', lifespan: 2, parameters: { city: 'Rome' }});
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
agent.handleRequest(intentMap);
});
错误:
TypeError: request is not a function
at welcome (/user_code/index.js:33: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:71: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)
您对 request
有两个定义。这是第一个:
var request = require('request');
这是第二个,它在函数体内屏蔽了第一个:
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
当您在函数体内调用 request() 时,JavaScript 认为您指的是最里面的 request
,这是传递给它的第一个参数。这显然不是您想要的。那时,request
不是函数。
只要给它们起不同的名字,这样它们就不会相互冲突。