Firebase了解snapshot.child() [英] Firebase understanding snapshot.child()

查看:39
本文介绍了Firebase了解snapshot.child()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑Firebase快速入门指南(此处)

consider this data structure referenced on the Firebase quick start guide (here)

{"name": {"first": "Fred","last": "Flintstone"}

文档说,人们可以使用以下方法访问从查询返回的每个名称"子对象的数据快照位置:

The docs say that one can access the datasnapshot location of each child object of "name" returned from a query using:

var ref = new Firebase("https://docs-examples.firebaseio.com/samplechat/users/fred");
ref.once("value", function(snapshot) {
     var nameSnapshot = snapshot.child("name");
     var name = nameSnapshot.val();

     name === { first: "Fred", last: "Flintstone"}

     var firstNameSnapshot = snapshot.child("name/first");
     var firstName = firstNameSnapshot.val();
     firstName === "Fred"

     var lastNameSnapshot = snapshot.child("name").child("last");
     var lastName = lastNameSnapshot.val();
     lastName === "Flintstone"

     var ageSnapshot = snapshot.child("age");
     var age = ageSnapshot.val();
     age === null (because there is no "age" child in the data snapshot)
});

但是这有点奇怪,是在处理以下几行时.

But what's a little weird about this is when the following lines are processed.

var nameSnapshot = snapshot.child("name");
var name = nameSnapshot.val();

还检索了

name.first name.last .那么为什么要使用这种快照方法"child()"呢?或者更确切地说,何时使用此方法会很有用,因为当您拉出父对象时,Firebase会拉出所有子对象,或者是否有一种方法可以在不拉出某些子对象的情况下检索父节点/对象?这样对我来说这种方法就有意义了.

name.first, and name.last are also retrieved. So why would one use this snapshot method "child()"? Or rather when would it be beneficial to use this method, since when you pull the parent object, Firebase pulls all children, or is there a way to retrieve a parent node/object without pulling some of it's children? Then this method to me would make sense.

任何信息将不胜感激!谢谢

Any information would be gratefully appreciated! Thanks

推荐答案

有没有一种方法可以检索父节点/对象而不拉一些子节点?

is there a way to retrieve a parent node/object without pulling some of it's children?

Firebase JavaScript API始终检索完整的节点.因此:不,JavaScript API中没有办法获得较浅的结果/

The Firebase JavaScript API always retrieves the complete node. So: no, there isn't a way in the JavaScript API to get a shallow result/

为什么要使用这种快照方法 child()?

如果我们将 snapshot.child("property") snapshot.val().property 进行比较. DataSnapshot.child()方法返回一个 DataSnapshot ,从中可以再次获取一个 ref . val()方法将快照的值反序列化为JSON.因此,如果需要一个引用,则必须构造自己的引用.但是每种方法的价值在很大程度上取决于您的用例,因此我无法为您回答为什么".

If we compare snapshot.child("property") with snapshot.val().property. The DataSnapshot.child() method returns a DataSnapshot, from which you can get a ref again. The val() method deserializes the snapshot's value into JSON. So you'll have to construct your own ref if you'd need one. But the value of each depends highly on your use-case, so "why" is not something I can answer for you.

这篇关于Firebase了解snapshot.child()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆