Differences between 'child_added' and 'value' firebase?

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?

Answers:

Answer

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.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.