使用Firebase函数进行数学计算

时间:2019-01-29 16:31:12

标签: firebase google-cloud-functions calculator calculation

使用firebase函数计算加,减,乘,除和mod结果,并且未定义作为响应而不是计算值。

代码

const functions = require('firebase-functions');

exports.calculator = functions.https.onRequest((request, response) => {

console.log("request.body.result.parameters: ", 
request.body.result.parameters);
let params = request.body.result.parameters; 

var sum = parseFloat(params.number1 + params.number2);
var sub = parseFloat(params.number1 - params.number2);
var mul = parseFloat(params.number1 * params.number2);
var div = parseFloat(params.number1 / params.number2);
var mod = parseFloat(params.number1 % params.number2);

if (params.operator === "addition" ) {
    response.send({
            speech:
            `Here is your answer: ${sum}`             
});
}

else if (params.operator === "subtraction" ) {
            response.send({
                    speech:
                    `Here is your answer: ${sub}`            
        });
    }
else if (params.operator === "multiplication" ) {
           response.send({
                   speech:
                   `Here is your answer: ${mul}`            
        });
   }
else if (params.operator === "division" ) {
    response.send({
            speech:
            `Here is your answer: ${div}`            
        });
   }
else if (params.operator === "modulus" ) {
    response.send({
            speech:
            `Here is your answer: ${mod}`            
        });
   }
});

响应: 这是您的答案:未定义

0 个答案:

没有答案