Mathematical calculation using firebase function

♀尐吖头ヾ 提交于 2019-12-25 01:35:11

问题


Calculating addition, subtraction, multiplication, division and mod result using firebase function and getting undefined as a response instead of the calculated value.

CODE

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

Response: Here is your answer: undefined


回答1:


Working fine now!

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


来源:https://stackoverflow.com/questions/54425584/mathematical-calculation-using-firebase-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!