如何从Firebase数据快照获取密钥? [英] How to get the key from a Firebase data snapshot?
问题描述
我可以使用电子邮件地址查询我的用户
数组并返回用户的帐户信息:
I'm able to query my users
array with an e-mail address and return the user's account info:
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'
...
如何获得密钥( -KiBBDaj4fBDRmSS3j0r
)。 snapshot.key
返回用户
。 snapshot.child('email')。key
返回电子邮件
。密钥似乎不是孩子,即它似乎在用户
和电子邮件之间
。
How do I get the key (-KiBBDaj4fBDRmSS3j0r
). snapshot.key
returns users
. snapshot.child('email').key
returns email
. The key doesn't appear to be a child, i.e., it appears to be in between users
and email
.
推荐答案
你可以做的这样的事情:
You could do something like this:
var key = Object.keys(snapshot.val())[0];
参考: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
Object.keys()方法返回给定对象自己的
可枚举属性的数组,其顺序与a提供的顺序相同
for ... in循环(不同之处在于for-in循环也枚举了原型链中的
属性)。
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).
这篇关于如何从Firebase数据快照获取密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!