Firebase return null if it does not exist

0

I have this function:

var db = firebase.firestore();
var perRef =
db.collection("personas").where("edad","==",33);
perRef.get()
.then(function(querySnapshot) { querySnapshot.forEach(function(doc) {
    console.log("data:" + doc.data().nombre );
});
})
.catch(function(error) {

    console.log("Error: " , error);

});

my problem is that I do not know how to return null if there is no document inside the foreach, and that document does exist I want to return it but without sending it to another function and I do not know how

    
asked by Gabriel Rodriguez 26.07.2018 в 19:52
source

1 answer

0

To check if what you are looking for in the reference exists, you can do the following

perRef.get()
.then(function(querySnapshot)
if (querySnapshot.hasChild(lainfoachequear)) {
    alert('existe');
  }

or as follows

perRef.get()
    .then(function(querySnapshot)
    if (querySnapshot.exists() {
        alert('existe');
      }
    
answered by 26.07.2018 в 21:26