AngularFire 检查值是否存在,如果不存在,则创建它
AngularFire check if value exists, if not, create it
我是 AngularFire 的新手,在更新到 AngularFire5 之后我遇到了一些问题。我想创建一个 'room',将有一个 id:name,所以在用户输入名称后,我检查数据库是否存在该名称,如果不存在,我将创建。我正在添加 InfoMessages 以指定是否创建了房间。
这是我的代码:
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');
};
})
}
问题是一旦检查是否有同名房间,创建后再次检查并显示错误消息。
那么,我如何告诉 Observable 一旦创建就必须停止?
您可以在插入数据后取消订阅,在您的 createNewGame
函数中,
this.afDB.list("/game/" + salaName).set("player1", this.player1.name);
this.items.unsubscribe();
我是 AngularFire 的新手,在更新到 AngularFire5 之后我遇到了一些问题。我想创建一个 'room',将有一个 id:name,所以在用户输入名称后,我检查数据库是否存在该名称,如果不存在,我将创建。我正在添加 InfoMessages 以指定是否创建了房间。
这是我的代码:
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');
};
})
}
问题是一旦检查是否有同名房间,创建后再次检查并显示错误消息。
那么,我如何告诉 Observable 一旦创建就必须停止?
您可以在插入数据后取消订阅,在您的 createNewGame
函数中,
this.afDB.list("/game/" + salaName).set("player1", this.player1.name);
this.items.unsubscribe();