Javascript 无法部署Firestore云功能
我正在尝试部署一个简单的Firestore云函数,但无法。编译器也没有告诉我错误是什么。有人能帮帮我吗。有些人说第二个参数不应该是通配符,但这样的事情毫无意义Javascript 无法部署Firestore云功能,javascript,node.js,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Node.js,Firebase,Google Cloud Firestore,Google Cloud Functions,我正在尝试部署一个简单的Firestore云函数,但无法。编译器也没有告诉我错误是什么。有人能帮帮我吗。有些人说第二个参数不应该是通配符,但这样的事情毫无意义 exports.checkIfScannerExists = functions.firestore.document('Scanner_Number_Check/{push_id}/Phone').onWrite((change, context) => { if(change.after.exists()) {
exports.checkIfScannerExists = functions.firestore.document('Scanner_Number_Check/{push_id}/Phone').onWrite((change, context) => {
if(change.after.exists())
{
const push_id = context.params.push_id;
const phone_number = change.after.val();
admin.firestore().collection('Store_Logins').where('Phone', '==', phone_number).get()
.then(snapshot => {
if(snapshot.empty)
{
admin.firestore().collection('Scanner_Number_Check').collection(push_id).collection('Response').set("No")
return;
}
else
{
admin.firestore().collection('Scanner_Number_Check').collection(push_id).collection('Response').set("No")
return;
}
})
}
return null;
})
您没有给出任何错误详细信息,但您的云函数中有几个错误:
- 您没有正确管理云功能生命周期。在调用异步方法的后台云函数中返回一个承诺,告诉CF平台在清理函数之前等待该承诺被履行或拒绝。有关更多详细信息,请参阅
- 您需要调整用于声明函数的路径:“触发器必须始终指向文档,即使使用通配符”(请参阅)
- 您应该使用而不是
val()
- 执行
将不起作用。您可能需要执行db.collection('Scanner\u Number\u Check')。collection(push\u id)。collection('Response')。set(“No”)
db.collection('Scanner\u Number\u Check').doc(push\u id).set({Response:“No”})
因此,以下方法应该有效(未经测试):
你遇到了什么错误?编译器显示了什么??此外,请注意,您没有正确返回承诺链。此外,如果执行相同的操作,为什么要使用
if
,无论if
检查的结果如何?抱歉,我把if语句弄糟了,因为它只是一个基本示例。是的,我的承诺没有得到正确的回报。谢谢此外,如果路径中的节点数为奇数,则似乎无法实现onWrite函数。裁判:是的!我没抓到这个。。。正如“即使使用通配符,触发器也必须始终指向文档”中所说的那样,答案是正确的!
exports.checkIfScannerExists = functions.firestore.document('Scanner_Number_Check/{push_id}').onWrite((change, context) => {
if (change.after.exists()) {
const db = admin.firestore();
const push_id = context.params.push_id;
//const phone_number = change.after.val(); // There isn't any val() method, this is for the RTDB
const phone_number = change.after.data().phone_number;
return db.collection('Store_Logins').where('Phone', '==', phone_number).get()
.then(snapshot => {
if (snapshot.empty) {
return db.collection('Scanner_Number_Check').doc(push_id).set({ Response: "No" })
}
else {
// return db.collection('Scanner_Number_Check').collection(push_id).collection('Response').set("No")
// You are doing the same thing than above... probably to be adapted
return db.collection('Scanner_Number_Check').doc(push_id).set({ Response: "Yes" })
}
})
} else {
return null;
}
})