makitocode
makitocode

Reputation: 948

if statement variable == null not found with Firebase?

I stumbled with this if statement and i dont know why doesn't work.

In my firebase database doesn't exists de collection Cuisine.

return db.ref('Global/Cuisine').once('value').then(cuisineList =>{
     console.log(`Value cuisineList --> ${JSON.stringify(cuisineList)}`);
     //print Value cuisineList --> null
     if(cuisineList == null){
         console.log(`Doesn't exists`);
         //Do stuff

     }else{
         console.log(`Exists`);
         //do stuff
     }
})

and always print Exists... why ?, the value of code is null. even, when I use other if statement

if(!cuisineList)

or

if(cuisineList === null)

print Exists.

but... when I get the value of snapshot of firebase it works...

return db.ref('Global/Cuisine').once('value').then(snapcuisineList =>{
   let cuisineList = snapcuisineList.val();
   console.log(`Valor cuisineList --> ${JSON.stringify(cuisineList)}`);
   if(cuisineList == null)
        ...

outside of the technical explanation of firebase .val(), the value is null, why doesn't work?

Upvotes: 0

Views: 123

Answers (1)

5ar
5ar

Reputation: 2210

I don't have much experience with firebase, but looking at this, I'd say that you have to use .val() to get null. And the reason why JSON.stringify gives null is because DataSnapshot implements toJson

Upvotes: 1

Related Questions