使用firebase函数的数学计算

问题描述 投票:0回答:1

使用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}`            
        });
   }
});

回复:这是你的答案:未定义

firebase google-cloud-functions calculator calculation
1个回答
3
投票

现在工作正常!

const functions = require('firebase-functions');
exports.calculatorbot = functions.https.onRequest((request, response) => {    
console.log("request.body.result.parameters: ", request.body.result.parameters);    
let params = request.body.result.parameters;
console.log("params: ", params)
var sum = Number(params.number1) + Number(params.number2);
var sub = Number(params.number1) - Number(params.number2);
var mul = Number(params.number1) * Number(params.number2);
var div = Number(params.number1) / Number(params.number2);
var mod = Number(params.number1) % Number(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}`
    });
}
else {
    response.send({
        speech:
            `Invalid operator! I can perform addition, multiplication, division, 
modulus and subtraction till now.`
    });
}
});
© www.soinside.com 2019 - 2024. All rights reserved.