问题
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