db触发器引发的firebase函数引发的函数未定义,预期的承诺或值

时间:2018-11-28 19:46:28

标签: firebase firebase-realtime-database google-cloud-functions

我基于实时数据库触发器的firebase函数如下所示

exports.on_user_created = functions.database.ref("/users/{id}")
    .onCreate((change, context) => {
        console.log("start of on_user_created ")   
        const user = change.val();
        console.log("New user:::" + JSON.stringify(user))

        const uid = user._uid
        const referralCode = user._referralCode

        console.log("creating referral node for uid:" + uid + " with code:" + referralCode)

        if(referralCode === undefined){
            console.error("No referral code created for the user while sign up. Referral node cannot be created.")    
            return true
        }


        var db = admin.database();
        var ref = db.ref('referrals')

        ref.child(referralCode).set({"uid": uid}).then(
            (resp) => {
                        console.log("referral node created")
                        return true
                      }
          ).catch(
            (err) => {
                      console.error("unable to create referral node on user create:" + err)    
                      return true  
            }
        )    
})

在运行时抛出

 5:47:02.035 AM on_user_created  Function returned undefined, expected Promise or value 

我不明白为什么

1 个答案:

答案 0 :(得分:1)

根据以下Doug的注释进行了调整:“如果您没有异步工作要做,通常返回null

这是因为您没有返回set()异步操作返回的Promise。

您应该执行以下操作:

exports.on_user_created = functions.database.ref("/users/{id}")
    .onCreate((change, context) => {
        console.log("start of on_user_created ")   
        const user = change.val();
        console.log("New user:::" + JSON.stringify(user))

        const uid = user._uid
        const referralCode = user._referralCode

        console.log("creating referral node for uid:" + uid + " with code:" + referralCode)

        if(referralCode === undefined){
            console.error("No referral code created for the user while sign up. Referral node cannot be created.")    
            return null  // <-- See Doug's comment below.
        }

        var db = admin.database();
        var ref = db.ref('referrals')

        return ref.child(referralCode).set({"uid": uid}).then( // <-- !! Here we return
            (resp) => {
                        console.log("referral node created")
                        return null
                      }
          ).catch(
            (err) => {
                      console.error("unable to create referral node on user create:" + err)    
                      return null  
            }
        )    
})

请注意,如果您不需要console.log(),例如用于生产。

exports.on_user_created = functions.database.ref("/users/{id}")
    .onCreate((change, context) => { 
        const user = change.val();
        const uid = user._uid
        const referralCode = user._referralCode

        if (referralCode === undefined) {
            return null;
        } else {
            var db = admin.database();
            var ref = db.ref('referrals')

            return ref.child(referralCode).set({"uid": uid});
        }

});

要详细了解在Cloud Function中兑现承诺的重要性,我建议您观看官方视频系列,尤其是标题为“ Learn JavaScript Promises”的视频:https://firebase.google.com/docs/functions/video-series/