javascript - Differences between 'child_added' and 'value' firebase? - Stack Overflow

I am seeing a tad difference between child_added and value when returning data in firebase. Using value

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?

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?

Share Improve this question edited Nov 6, 2016 at 6:37 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges asked Nov 5, 2016 at 5:44 ChipeChipe 4,81110 gold badges40 silver badges65 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744268020a4565959.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信