vendredi 25 août 2017

How to check existing of object? (hasChild, isExist?)

I have a code:

var ref = firebase.database().ref('names');
    
    let objs = [];
    ref.once('value').then((snap) => {

      objs.push(snap.val());

      console.log('before ', objs);
      if(snap.val() == null){

        objs = [{'111': 4444}];
        ref.set(objs[0]);
      }else if(snap.val()[0] == undefined){

        objs.push({'222': 5466});
        ref.set(objs);

      }else{

        objs[0].push({'333': 66776});
        ref.set(objs[0]);
      }
      
      console.log('after ', objs);

//at the beginning 'names' dosen't exist at all

      //before  [ null ]
      //after  [ { '111': 4444 } ]

      //before  [ { '111': 4444 } ]
      //after  [ { '111': 4444 }, { '222': 5466 } ]
      
      //before  [ [ { '111': 4444 }, { '222': 5466 } ] ]
      //after  [ [ { '111': 4444 }, { '222': 5466 }, { '333': 66776 } ] ]
      
    });

At the beginning 'names' dosen't exist, How to check properly this case? It's possable to add objects better than I prinetd. Help please, if you can, refactor please Thanks




Aucun commentaire:

Enregistrer un commentaire