response.send不是函数

时间:2019-03-22 17:01:09

标签: node.js firebase google-cloud-functions

我正在编写用于实现dialogflow chatbot的webhook的firebase函数。它会不断生成错误,指出response.send不是函数

const functions = require('firebase-functions');
var request1 = require('request')
exports.webhook = functions.https.onRequest((request, response) => {
        console.log("request.body.result.parameters: ", request.body.result.parameters);
        let params = request.body.result.parameters;
        var options = {
            url: `https://islam360api.herokuapp.com/${params.find}`,
            json:true
        }
        request1(options, function(error, response, body){
            if(error) response.send({speech: "error in API call"});
            else response.send({speech: body.speech});
        });
    });

Firebase日志 enter image description here

1 个答案:

答案 0 :(得分:2)

问题:这是影子变量名称的问题,当您尝试使用Firebase函数响应对象发送响应时,实际上您是在npm请求模块的响应对象上发送响应当然不可能

解决方案: 只需添加下划线或更改拼写,就可以开始使用代码了: (请注意从底部开始的第5行更改)

const functions = require('firebase-functions');
var request1 = require('request')
exports.webhook = functions.https.onRequest((request, response) => {
        console.log("request.body.result.parameters: ", request.body.result.parameters);
        let params = request.body.result.parameters;
        var options = {
            url: `https://islam360api.herokuapp.com/${params.find}`,
            json:true
        }
        request1(options, function(error, _response, body){
            if(error) response.send({speech: "error in API call"});
            else response.send({speech: body.speech});
        });
    });