2

I am new on AngularFire, and after the update to AngularFire5 I am having some issues. I want to create a 'room', the one which will have a id:name, so after the user inputs the name, I've check on the database if this name exists, if not, I will create. I am adding InfoMessages to specify if the room is created or not.

Here is my code:

checkIfRoomExistAndCreate(salaName: string, puntuacio: number){
    this.items = this.afDB.list("/game/" + salaName).valueChanges();
    this.items.subscribe((x) => {
        if (x.length === 0) {
            this.createNewGame(salaName, puntuacio);
        } else {
            console.log('This room already exists');
        };
    })
}

The problem is once is checked if there is a room with the same name, after created it, it check again and the error message is displayed.

So, how can I tell to the observable that once is created, it has to stop?

Vega
  • 27,856
  • 27
  • 95
  • 103
Joan
  • 233
  • 1
  • 5
  • 17

1 Answers1

0

You can unsubscribe once the data is inserted, inside your createNewGame function,

this.afDB.list("/game/" + salaName).set("player1", this.player1.name);
this.items.unsubscribe();
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • my items are a Observable, and try to unsubscribe(); doesen't appear as an option. – Joan Oct 22 '17 at 18:06
  • Yes, in this sample, there were a question like mine, and what they did were save in a variable the subscription, and then, this variable can be unsucribed. Btw, why there is no more the method .first() on the angularfire? – Joan Oct 22 '17 at 18:15
  • what do you mean by .first() ? – Sajeetharan Oct 22 '17 at 18:16