firbase的云功能,例如:
exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onWrite(event => {
//how to write data at another node and return promise?
return admin.database().ref(`/abc/1234`).update({a:1}); //is this the correct way?
})
在https://firebase.google.com/docs/functions/get-started中,它说
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
// Setting an "uppercase" sibling in the Realtime Database returns a Promise.
但在https://firebase.google.com/docs/database/admin/save-data中,API使用了回调。我可以知道如何正确设置/更新Firebase内部功能吗?该代码会起作用,但我不确定我是否正确地进行操作或是否是建议的方法。谢谢。
firebase数据库中的每个写操作都会返回承诺,以便您可以将其链接或返回Google Cloud Functions环境。请参阅Michael提到的博客文章:遵守我们的承诺(和回调(。
,这两个代码片段将执行相同的操作。带回调:
var ref = admin.database().ref(`/abc/1234`);
ref.update({a:1}, function(error) {
if (!error) {
console.log("Write completed")
}
else {
console.error("Write failed: "+error)
}
});
有承诺:
var ref = admin.database().ref(`/abc/1234`);
ref.update({a:1}).then(function() {
console.log("Write completed")
}).catch(function(error) {
console.error("Write failed: "+error)
});