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會清理乾淨。 –

相關問題