1

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

我有以下数据结构:

screenshot

而这种伪代码:

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' 已经存在。我如何实现这一目标?

回答

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 

}) 
+0

你也将要返回来自更新的承诺(),以确保更新前是完整的云功能不会清理功能。 –

+0

绝对!了解承诺对于实施Firebase云端功能至关重要。 @Jonathan Doe,请查看[Jen Person's Firecasts](https://www.youtube.com/playlist?list=PLl-K7zZEsYLkPZHe41m4jfAxUi0JjLgSM)。他们有很大的帮助! –

+0

@RohanStark请在回答中加入'return',因为在更新完成之前,有时候Cloud Functions会清理干净。 –

相关问题