【问题标题】:Check if name exists using Cloud Functions for Firebase使用 Cloud Functions for Firebase 检查名称是否存在
【发布时间】:2017-07-02 14:57:16
【问题描述】:

如何使用 Cloud Functions for Firebase 检查名称是否存在?

我有以下数据结构:

还有这个伪代码:

exports.addIt = functions.database.ref('/messages/{pushId}')
.onWrite(event => {

if(creationDate does not exist)
event.data.adminRef.update({"creationDate":Date.now()})

else // do not update anything

})

我想检查“creationDate”是否已经存在。我如何做到这一点?

【问题讨论】:

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


    【解决方案1】:

    类似这样的:-

    exports.addIt = functions.database.ref('/messages/{pushId}')
    .onWrite(event => {
    if(typeof event.data.val().creationDate != 'undefined')
    return event.data.adminRef.update({"creationDate":Date.now()})
    
    else // do not update anything
    
    })
    

    【讨论】:

    • 您还需要返回来自 update() 的承诺,以确保 Cloud Functions 在更新完成之前不会清理该函数。
    • 绝对!了解 Promise 是实现 Firebase Cloud Functions 的必要条件。 @Jonathan Doe,请查看 Jen Person's Firecasts。他们很有帮助!
    • @RohanStark 请在您的回答中包含return,因为有时 Cloud Functions 会在更新完成之前清理。
    猜你喜欢
    • 2022-01-15
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 2018-01-14
    相关资源
    最近更新 更多