node.js - 如何从服务器端的 Firebase 数据库检索最后创建的对象

标签 node.js firebase firebase-realtime-database google-cloud-functions

我是 Firebase 云功能的新手,如果这非常简单,我很抱歉。我正在尝试接收数据库中最新推送的对象。不幸的是,此代码返回已推送到数据库“/customers”中的每个对象。我已经尝试过 .onCreate 但根本没有做任何事情。

exports.newCustomer = functions.database.ref('/customers/').onWrite(event => {
  console.log(event.data.val());
});

enter image description here

最佳答案

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/

相关文章:

flutter - Flutter 中的 Firebase 后台消息处理程序产生重复的隔离并运行应用程序两次

android - 无法解析符号 'FirebaseListAdapter'

android - 无法从 Firebase 读取

mysql - 我应该在 NodeJs 中使用什么模块进行分页,在 Mysql 后端使用 Express JS?

javascript - 如何在 Jest 测试函数中模拟变量?

java - 如何将数据从java应用程序保存到firebase

Android - NullPointerException 尝试调用虚拟方法 'com.google.android.gms.tasks.Task

javascript - 将js对象写入nodejs中的文件(包括方法)?

node.js - 使用 $redact 过滤多个数组

javascript - Firebase 与 Web : Using user Id in reference causes error: Uncaught TypeError: Cannot read property 'on' of undefined