我在部署函数时遇到错误,因为“在解析函数触发器时发生错误”,并且在firestore.collection('orders').get()
行的syntax error: unexpected token '.'
语法错误。我的节点版本是v8.11.3。
我尝试使用命令更新节点版本:
npm install -g firebase-tools@latest
,但错误仍然保持不变。你能帮我这个忙吗?
这是我的代码。
const functions = require('firebase-functions');
var admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
var firestore = admin.firestore();
exports.webhook = functions.https.onRequest((request, response) => {
console.log("request.body.result.parameters: ", request.body.result.parameters);
switch(request.body.result.action)
{
case'bookhotel':
let params = request.body.result.parameters;
firestore.collection('orders').add(params)
.then((docRef) => {
console.log("docRef: ", docRef);
response.send({
speech:
`${params["name"]} your hotel booking request for ${params["room-type"]} room is forwarded for ${params["persons"]} persons,
we will contact you on ${params["email"]} soon.`
});
return;
})
.catch((e => {
console.log("error:", e);
response.send({
speech: "something went wrong when writing on database"
});
}))
break;
case'showbooking':
firestore.collection('orders').get()
.then((querySnapshot))=>{
var orders=[];
querySnapshot.forEach((doc)=>{orders.push(doc.data()) });
var speech=`you have ${orders["length"]} orders \n`;
orders.forEach((eachOrder,index)=>{
speech=`number ${index+1} is ${eachOrder["room-type"]} room for
${eachOrder["persons"]} persons, ordered by ${eachOrder["name"]},
contact email is ${eachOrder["email"]}`
})
response.send({
speech:speech
});
})
.catch((err)=>{
console.log('error getting documents',err);
response.send({
speech: "something went wrong when reading from database"
})
})
break;
default:
response.send({
speech:"no action matched in webhook"
})
}
});
答案 0 :(得分:0)
我将您的代码复制到VS Code中,这给了我一个不同的错误。
首先,您需要在保护套周围加上花括号。代替这个:
switch (thing) {
case 'option1':
statement;
statement;
break:
}
应该是这样的:
switch (thing) {
case 'option1': {
statement;
statement;
break:
}
}
请注意语句块周围的花括号。
此行上的括号也太多:
.then((querySnapshot))=>{
应该是:
.then((querySnapshot)=>{
之后,代码可以正常编译。