Javascript 如何从 Firebase 数据快照中获取密钥?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43615466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the key from a Firebase data snapshot?
提问by Thomas David Kehoe
I'm able to query my usersarray with an e-mail address and return the user's account info:
我可以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'
...
How do I get the key (-KiBBDaj4fBDRmSS3j0r). snapshot.keyreturns users. snapshot.child('email').keyreturns email. The key doesn't appear to be a child, i.e., it appears to be in between usersand email.
我如何获得密钥 ( -KiBBDaj4fBDRmSS3j0r)。snapshot.key返回users。snapshot.child('email').key返回email。键似乎不是一个孩子,即它似乎在users和之间email。
回答by camden_kid
You could do something like this:
你可以这样做:
var key = Object.keys(snapshot.val())[0];
Ref: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
参考: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).
Object.keys() 方法返回给定对象自己的可枚举属性的数组,其顺序与 for...in 循环提供的顺序相同(区别在于 for-in 循环将原型链中的属性枚举为好)。
回答by Ernestyno
Realtime database:
实时数据库:
For this you can simple use: snapshot.key
为此,您可以简单地使用:snapshot.key
snapshot = firebase.database.DataSnapshot
快照 = firebase.database.DataSnapshot
this.app.database()
.ref('/data/')
.on('value', function(snapshot) {
const id = snapshot.key;
//----------OR----------//
const data = snapshot.val() || null;
if (data) {
const id = Object.keys(data)[0];
}
});
Firestore:
消防店:
snapshot.id
快照.id
snapshot = firebase.firestore.DocumentSnapshot
快照 = firebase.firestore.DocumentSnapshot
this.app.firestore()
.collection('collection')
.doc('document')
.onSnapshot(function(snapshot) {
const id = snapshot.id;
//----------OR----------//
const data = snapshot.data() || null;
if (data) {
const id = Object.keys(data)[0];
}
});
回答by Thomas David Kehoe
Similar to camden_kid, I used Object.keys(arr), but in three lines:
与 camden_kid 类似,我使用了Object.keys(arr), 但在三行中:
var arr = snapshot.val();
var arr2 = Object.keys(arr);
var key = arr2[0];
console.log(key) // -KiBBDaj4fBDRmSS3j0r
回答by Renaud Tarnec
users.orderByChild('email').equalTo(authData.user.email)is a Query (doc) that you have built by "chaining together one or more of the filter methods". What is a bit specific with your query is that it returns a dataSnapshot with only one child, since you query with equalTo(authData.user.email).
users.orderByChild('email').equalTo(authData.user.email)是您通过“将一个或多个过滤器方法链接在一起”构建的查询 ( doc)。您的查询有一点特别之处在于它返回一个只有一个孩子的 dataSnapshot,因为您使用equalTo(authData.user.email).
As explained here, in this exact case, you should loop over the returned dataSnapshot with forEach():
正如这里所解释的,在这种确切的情况下,您应该使用以下命令遍历返回的 dataSnapshot forEach():
Attaching a value observer to a list of data will return the entire list of data as a single snapshot which you can then loop over to access individual children.
Even when there is only a single match for the query, the snapshot is still a list; it just contains a single item. To access the item, you need to loop over the result, as follows:
ref.once('value', function(snapshot) { snapshot.forEach(function(childSnapshot) { var childKey = childSnapshot.key; var childData = childSnapshot.val(); // ... }); });
将值观察者附加到数据列表将返回整个数据列表作为单个快照,然后您可以循环访问单个子项。
即使查询只有一个匹配项,快照仍然是一个列表;它只包含一个项目。要访问该项目, 您需要遍历 result,如下所示:
ref.once('value', function(snapshot) { snapshot.forEach(function(childSnapshot) { var childKey = childSnapshot.key; var childData = childSnapshot.val(); // ... }); });
回答by Karan Prajapati
I found new way to get the data based on snapshot key -
我找到了基于快照键获取数据的新方法 -
firebase.database().ref('events').once('value',(data)=>{
//console.log(data.toJSON());
data.forEach(function(snapshot){
var newPost = snapshot.val();
console.log("description: " + newPost.description);
console.log("interest: " + newPost.interest);
console.log("players: " + newPost.players);
console.log("uid: " + newPost.uid);
console.log("when: " + newPost.when);
console.log("where: " + newPost.where);
})
})


