【问题标题】:Unable to deploy Firestore Cloud Function无法部署 Firestore 云功能
【发布时间】:2020-10-15 18:21:51
【问题描述】:

我正在尝试部署一个简单的 Firestore Cloud 功能,但无法部署。编译器也没有告诉我错误是什么。有人可以帮我吗。有人说第二个参数不应该是通配符,但这样的事情是零意义的。

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

【问题讨论】:

  • 您遇到了哪个错误?编译器显示什么?此外,请注意您没有正确返回承诺链。
  • 另外,如果您执行相同的操作,无论if 检查的结果是什么,为什么还要使用if
  • 抱歉,我搞砸了 if 语句,因为它只是一个基本示例。是的,我的承诺没有得到适当的回报。谢谢!
  • 另外,如果路径中的节点数是奇数,您似乎无法实现 onWrite 函数。参考:stackoverflow.com/questions/46639058/…
  • 是的!我没有抓住这个...正如doc 中所说的“您的触发器必须始终指向一个文档,即使您使用的是通配符。”答案改编!

标签: javascript node.js firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

您没有提供任何错误详情,但您的 Cloud Function 中有几个错误:

  • 您没有正确管理 Cloud Function 生命周期。在后台返回 Promise Cloud Functions 调用异步方法会告诉 CF 平台等到 Promise 被履行或被拒绝后再清理函数。有关详细信息,请参阅https://firebase.google.com/docs/functions/terminate-functions
  • 您需要调整用于声明函数的路径:“触发器必须始终指向文档,即使您使用的是通配符”(请参阅​​doc
  • 您应该使用data() 而不是val()
  • 执行db.collection('Scanner_Number_Check').collection(push_id).collection('Response').set("No") 将不起作用。你可能想做db.collection('Scanner_Number_Check').doc(push_id).set({ Response: "No" })

所以,以下应该可以工作(未经测试):

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

})

【讨论】:

    猜你喜欢
    • 2018-12-29
    • 2021-11-17
    • 2021-01-30
    • 2018-08-09
    • 2017-08-15
    • 2021-02-26
    • 2020-09-04
    相关资源
    最近更新 更多