node.js - 查询Firebase并获取Key

标签 node.js express firebase firebase-realtime-database

我正在尝试查询 firebase,如果记录存在,我想要唯一的 key ,但似乎无法获取它。我的过滤器工作正常,但我不知道对象属性名称是什么,并且似乎无法在 firebase 上找到任何有帮助的内容。 .key() 总是返回错误。这是我返回对象时的样子

GET /js/init.js 200 5.048 ms - 207
GET /img/logo.png 200 2.376 ms - 2793
{ '-KP8qXgQRaeClqzKX1Lo': { email: 'bdole@gmail.com' } }
GET /sent 200 7.923 ms - 8110

这是我的代码

    firebase.database().ref('/customers').orderByChild("email").equalTo(email).on("value", function(snapshot) {
      console.log(snapshot.val());
    },function (errorObject) {
      console.log("The read failed: " + errorObject.code);
    });

我需要该 key ,因为如果它存在,我需要在下一步中使用它。

最佳答案

您可以使用child_added来跟踪新添加的记录并检索 key 。

firebase.database().ref('customers').on('child_added', function(snapshot) {
    console.log(snapshot.key); //This will print that unique key
});

关于node.js - 查询Firebase并获取Key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38944541/

相关文章:

java - Android Firebase : Hashmap can't be cast to a given Object type

javascript - 尝试将 haml-coffee 与expressJS 应用程序一起使用时出现奇怪的错误

ios - TableView 对所有用户公开

javascript - Nodejs 编码为 Base64 字符串不起作用

json - express.json 与 bodyParser.json

node.js - 使用 Socket.io [Express, Socket.io, Sequelize] 发出 Sequelize 结果时出错

json - postman - 发布应用程序/json 数据

javascript - Firebase - Cloud Firestore 触发器幂等性

node.js - 使用 Nodejs mscdex/ssh2 的 root 密码

javascript - 在 Node API 混淆中使用 knex.js 编写插入语句