'child_added'和'value'firebase之间的差异? [英] Differences between 'child_added' and 'value' firebase?

查看:110
本文介绍了'child_added'和'value'firebase之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在firebase中返回数据时,我发现 child_added 之间有一点区别。使用我可以测试 snapshot.val()是否已经返回了一些或者没有使用它: / p>

获取数据:

  ref.orderByChild('appUserName')
.equalTo(issuer)
.once('value')
.then(function(snapshot){
var value = snapshot.val();
if(b)价值){
//有价值...
}否则{
//没有价值......
}

数据结构:

  AppUsers  -  
234jl23jl4kj23 -
数据 -
// ..数据
userName:testUser1,
userPass:password123
9873h23foiu34u
//相同结构
o8987s52df134o
//相同结构

如果我 console.log 值的返回方式 snapshot.val()它返回生成密钥级别的数据:

  {234jl23jl4kj23  -  
{data -
// .. data
userName:testUser1,
userPass:password123}}

如果我使用 child_added获取数据

  ref.orderByChild('appUserName')
.equalTo(issuer)
.once('child_added')
.then(function(snapshot){
var value = snapshot.val();
if(value){
//有价值...
}否则{
//没有价值......
}

如果<$ c $,它甚至不会进入 .then 函数c> issuer 未找到 appUserName 的值,所以我无法在firebase函数中看到它是否有值。另外 child_added 获取数据的方式更深一层。它不是返回生成的键,而是返回该键中的值:

  {data  -  
//。 .data
userName:testUser1,
userPass:password123}

我更喜欢这样使用它,因为它将是一个较少的循环我必须检查以首先获取密钥内的数据,然后循环数据点中的对象。但是,如果 issuer 不在 appUserName 中,它将不会进入函数,甚至可以执行 if else



有没有办法深入 child_added 没有循环但仍能够执行 if else 来检查 snapshot.val( )有什么?

解决方案

child_added 在您查询的节点下为每个匹配的子项触发事件。如果没有匹配的子节点,它将不会触发。



这意味着要测试节点是否存在,您必须使用处理程序。由于查询可以有多个结果,因此您需要循环遍历子项。


I am seeing a tad difference between child_added and value when returning data in firebase. Using value I can test to see if snapshot.val() has returned something or not using it like this:

Fetching Data:

ref.orderByChild('appUserName')
    .equalTo(issuer)
    .once('value')
    .then(function (snapshot) {
      var value = snapshot.val();
      if (value) { 
          // Has Value...
      }else{
          //Has No Value...
      }

Data Structure:

AppUsers --
    234jl23jl4kj23 --
        data --
            //.. data
        userName : "testUser1",
        userPass: "password123"
    9873h23foiu34u
        //same structure
    o8987s52df134o
        //same structure

If I console.log how the value has returned snapshot.val() it returns the data on the level of the generated key:

{234jl23jl4kj23 --
    {data --
        //.. data
    userName : "testUser1",
    userPass: "password123"}}

If I get the data using child_added:

ref.orderByChild('appUserName')
    .equalTo(issuer)
    .once('child_added')
    .then(function (snapshot) {
      var value = snapshot.val();
      if (value) { 
          // Has Value...
      }else{
          //Has No Value...
      }

It wont even go into the .then function if issuer is not found as a value to appUserName, so I cant see in the firebase function if it got a value or not. Also the way child_added gets data is one level deeper. Instead of returning at the generated key it returns the values in that key:

{data --
   //.. data
userName : "testUser1",
userPass: "password123"}

I would prefer to use it this way because it would be one less loop I would have to check to first get the data inside of the key, then loop through the objects in the data spot. However if issuer is not in appUserName it wont go into the function for me to even do an if else

Is there a way to drill in as deep as child_added without looping but still be able to do an if else to check if the snapshot.val() has anything?

解决方案

The child_added event fires for each matching child under the node that you query. If there are no matching children, it will not fire.

This means that to test whether a node exists, you must use a value handler. And since a query can have multiple results, you will need to loop over the children.

这篇关于'child_added'和'value'firebase之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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