我可以使用电子邮件地址查询我的 users
数组并返回用户的帐户信息:
users.orderByChild('email').equalTo(authData.user.email).once('value').then(function(snapshot) {
console.log(snapshot.val());
console.log(snapshot.key); // 'users'
console.log(snapshot.child('email').key); 'email'
...
如何获取 key (-KiBBDaj4fBDRmSS3j0r
)。 snapshot.key
返回users
。 snapshot.child('email').key
返回 email
。该 key 似乎不是子项,即它似乎位于 users
和 email
之间。
最佳答案
你可以这样做:
var key = Object.keys(snapshot.val())[0];
引用号:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
关于javascript - 如何从 Firebase 数据快照中获取 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43615466/