0

I am doing a login system and im querying the entry by the user to verify if it exists on firestore, if it finds his his input, then i log it onto the console. Until this part all is well, when he inputs something that exists, it brings his data. However, when he inputs something that does NOT exists it inputs an empty array "[]" which means that there's no such thing. My problem is very simple, i can't find the proper logic to console log when his input does not exist and thats pretty much it.

Here's my code

  pageLogin() {
    var auxint = 0;
        this.dataAux
        let auxString = '[';
    var query = firebase.firestore().collection("armazenaTest")
    query.where('Documento.login', '==', this.User.login).get().then(res => {
      res.forEach(item => {
        if (item.exists) {
          auxint++;
          auxString += '{"id":"' + item.id + '","armazenaTest":' + JSON.stringify(item.data()) + '}';
          if(item.get('Documento.login') == this.User.login){
            console.log('Document found!: ', item.data())
          }
        }
        if (res.size != auxint)
          auxString += ', ';
      })
      auxString += ']';
      this.dataJSON = JSON.parse(auxString);
      console.log(this.dataJSON);
    }).catch(err => {
      console.log('An error occured ' + err);
    });

  }
xnok
  • 299
  • 2
  • 6
  • 35

1 Answers1

2

It should be as simple as checking your result before iterating through it:

query.where('Documento.login', '==', this.User.login).get().then(res => {
      if (res.length < 1) {
         console.log('no results!');
         //probably break or return here
      }
      res.forEach(item => {
        if (item.exists) {
          auxint++;
          auxString += '{"id":"' + item.id + '","armazenaTest":' + JSON.stringify(item.data()) + '}';
          if(item.get('Documento.login') == this.User.login){
            console.log('Document found!: ', item.data())
          }
        }
Austin T French
  • 5,022
  • 1
  • 22
  • 40