Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 214
Chipe Avatar asked Nov 05 '16 05:11

Chipe


People also ask

Is Firebase a key-value database?

Firebase uses a very popular style of NoSQL database: Key-value stores. The concept is not so complicated once you know the basic JSON syntax.

What is the difference between Realtime database and storage in Firebase?

*Realtime database store data only json format and it is specially used in app where data is synchronized concurrently like ola app(user location),sensex(Nifty) app where data not persist . *Firebase Storage just only store data like memory card.It is specially used for store backend data of app.

What is DataSnapshot in Firebase?

A DataSnapshot instance contains data from a Firebase Database location. Any time you read Database data, you receive the data as a DataSnapshot.


1 Answers

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.

like image 118
Frank van Puffelen Avatar answered Oct 13 '22 01:10

Frank van Puffelen