我是 Firebase 云功能的新手,如果这非常简单,我很抱歉。我正在尝试接收数据库中最新推送的对象。不幸的是,此代码返回已推送到数据库“/customers”中的每个对象。我已经尝试过 .onCreate 但根本没有做任何事情。
exports.newCustomer = functions.database.ref('/customers/').onWrite(event => {
console.log(event.data.val());
});
最佳答案
onWrite 事件应该只捕获新数据。不过,也许你应该更具体一些,如下所示:
exports.newCustomer = functions.database.ref('/customers/{randomID}').onWrite(event => {
console.log(event.data.val()); //here is the data that is added
console.log(event.params.randomID); // random ID
console.log(event.data.val().firstName); //change firstName to anything you added.
});
这将捕获对/customers/XXX/XXX/的任何写入事件
关于node.js - 如何从服务器端的 Firebase 数据库检索最后创建的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45382264/