我已经尝试过不同类型的解决方案来从Firebase-Database中删除子元素,但没有任何影响。我做错了什么?
我的数据库结构如下所示,其中players
位于数据库的根目录下
我有一个Angular 2单页面应用程序,其中 index.html 包含FireBase客户端初始化:
<script src="https://www.gstatic.com/firebasejs/3.5.3/firebase.js"></script>
<script>
// Initialize Firebase
var config = {/*....*/};
firebase.initializeApp(config);
</script>
无论Firebase documentation这两个删除都无效:
removePlayer1(id: any): Promise<Response> {
return firebase.database().ref("players").child(id).once('value')
.then((snap: any) => {
console.log("Player was removed", id, snap);
snap.remove();
}).catch((err: any) => this.handleError(err));
}
removePlayer2(id: any): Promise<Response> {
return firebase.database().ref().child('players')
.child(id).remove().then((r: any) => {
console.log("Player was removed", id);
}).catch((err: any) => this.handleError(err));
}
This solution仍无效:
removePlayer(id: any): Promise<Response> {
var updates = {};
updates[id] = null;
return firebase.database().ref("players").update(updates)
.then((r: any) => {
console.log("Player was removed", id, r);
}).catch((err: any) => this.handleError(err));
}
同时更新和插入子值完美无缺:
savePlayer(player: IPlayer): Promise<Response> {
let playerRef: any;
if (!player._id) {
playerRef = firebase.database().ref().child('players').push().set(player);
} else {
var updates = {};
updates[player._id] = player;
playerRef = firebase.database().ref().child('players').update(updates);
}
return playerRef.catch((err: any) => this.handleError(err));
}
谢谢!
答案 0 :(得分:0)
我不知道为什么来自StakOverflow的the mentioned link没有帮助我,但我就在附近,因此我的解决方案看起来像这样:
removePlayer(id: any): Promise<any> {
var ref = firebase.database().ref().child('players');
return new Promise<any>((resolve, reject) => {
ref.child(id).once("value", function (snapshot: any) {
var updates = {};
updates[snapshot.key] = null;
ref.update(updates).then((r: any) => {
resolve(r);
}).catch((e: any) => reject(e));
}).catch((e: any) => reject(e));
});
}