Duplicate Itens in Firebase Ionic 3

I have a list in my firebase and I need to verify if item exist in list, I would like a simple check but my loop doesn’t work

verificaTitulo() {
    let keepGoing = false;
    this.voteService.getVotes()
        .subscribe((res) => {
          if(res == '') {
            this.pegarTitulo.emit(this.titulo);
            this.tituloVerificado = true;
            keepGoing = true;
            console.log('If to be empty');
          } else {
            if(!keepGoing) {
              for(let i = 0; i < res.length; i++) {
                if(res[i].titulo == this.titulo) {
                  console.log(res[i])
                  console.log('already exist')
                  keepGoing = true;
                  // break;
                } else {
                  console.log('create');
                  keepGoing = true;
                  // break;
                }
              }
              // keepGoing = false;
            }
          }

        })
  }

inside loop I have two conditions if exist (‘already exist’) else (‘create’)

In console I see this result, so the console show the two results already exist and creat, what’s wrong ?

You’re treating asynchronous operations as if they were synchronous. That won’t ever work. Lose keepGoing entirely and figure out some way to restructure verificaTitulo so that it returns Observable<boolean>.

@rapropos in my service I have the following method to get all itens

  getVotes() {
    return this.db.list(this.PATH)
      .snapshotChanges()
      .map(changes => {
        return changes.map(c => ( {key: c.key , ...c.payload.val() } ));
      })
  }

this is correct ?

I’ve never used Firebase so I can’t speak to the internals, but all functions should have declared return types, and getVotes() does not.

I don’t now if to use rules in firebase would be the better way in this case , but I don’t to make its…

{
  "rules": {
    "users": {
      ".read": true,
      ".write": true
    },
    "votes": {
      ".read": true,
      ".write": true,
        "$uid": {
          "titulo": {
            ".validate": "newData.isString() && newData.val().length <= 12"
          }
        }
    }
  }
}