如何获得添加的孩子的快照索引? [英] How to get the index of snapshot on child added?
问题描述
...
myDataRef.on('child_added',function(snapshot){
//我想通过索引知道它是什么快照
});
...
我可以用什么来得到快照
在 myDataRef
上添加了子项?
在Firebase集合的情况下,孩子的姓名是,而不是他们的索引。您可以使用数字名称,但是您的代码片段不会显示您是否执行此操作。
如果您正在查找名称在 myDataRef
下标识孩子的东西),那么你可以从 snapshot.name()
获取它。
所以你也得到了当前孩子的名字:
$ p $ myDataRef.on('child_added',function(snapshot){
var nameOfThisChild = snapshot.name();
});
如果您将项目存储在具有数字名称的节点中,则可以将该名称解析为数字与:
parseInt(nameOfThisChild)
您也可以直接获取子快照的 ref
:
<$ p $ ();
//相当于myDataRef.child(snapshot.name() ))
});
一如既往:请务必阅读 Firebase如何处理数组。
...
myDataRef.on('child_added', function(snapshot){
//I would like to know what snapshot it is, by index
});
...
What can I use to get the index of snapshot
in myDataRef
on child added?
In the context of a Firebase collection children are identified by their name, not by their index. You can use numeric names, but your code snippet does not show whether you do that.
If you're looking for the name (the thing that identifies the child under myDataRef
) then you can get it from snapshot.name()
.
So you also get the name of the current child:
myDataRef.on('child_added', function(snapshot){
var nameOfThisChild = snapshot.name();
});
If you stored the item in a node with a numeric name, you can parse the name into a number with:
parseInt(nameOfThisChild)
You can also get the ref
of the child snapshot directly:
myDataRef.on('child_added', function(snapshot){
var refToThisChild = snapshot.ref();
// equivalent to myDataRef.child(snapshot.name())
});
As always: be sure to read the documentation on how Firebase handles arrays.
这篇关于如何获得添加的孩子的快照索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!