Angularfire2 删除所有具有匹配 UserId 的项目
Angularfire2 remove all items with matching UserId
我在 Ionic 应用程序中使用 angularfire2。我正在尝试从 firebase 节点中删除与用户 ID 匹配的所有条目。我有一个忠诚度积分节点,当我 运行 我的重置功能时,我想从与该用户相关的忠诚度积分节点中删除所有条目。
我已经设法将所有条目添加到一个名为 'myPoints' 的变量中,方法是将其添加到我的构造函数中:
this.angularfire.auth.subscribe(res => {
if (res != null) {
let userID = res.auth.uid;
this.af.list('/loyaltypoints', {
query: {
orderByChild: 'userId',
equalTo: userID,
}
}).subscribe(response => {
this.myPoints = response;
})
}
})
我现在不知道如何 运行 this.myPoints 变量上的 remove() 函数来删除所有这些条目。
谢谢
更新
所以这似乎对我有用,首先像这样导入:
导入 'rxjs/add/operator/take';
然后此代码删除所有相关的 firebase 条目:
this.angularfire.auth.subscribe(res => {
if (res != null) {
let userID = res.auth.uid;
this.af.list('/loyaltypoints', {
preserveSnapshot: true,
query: {
orderByChild: 'userId',
equalTo: userID,
}
}).take(1).subscribe(response => {
console.log(response)
response.forEach((snapshot) => {
this.af.object('/loyaltypoints/' + snapshot.key).remove();
})
})
}
})
所以您缺少的是获取与每个忠诚度积分条目对应的密钥。
this.angularfire.auth.subscribe(res => {
if (res != null) {
let userID = res.auth.uid;
this.af.list('/loyaltypoints', {
preserveSnapshot: true,
query: {
orderByChild: 'userId',
equalTo: userID,
}
}).take(1).subscribe(snaphots=> {
snapshots.forEach((snapshot) => {
this.af.object('/loyaltypoints/' + snapshot.key).remove();
})
})
}
})
如果您注意到我添加了一个 take(1),因为如果您也在更改您订阅的节点,它会再次触发数据提取。您可能需要稍微尝试一下以确保它是正确的。
我在 Ionic 应用程序中使用 angularfire2。我正在尝试从 firebase 节点中删除与用户 ID 匹配的所有条目。我有一个忠诚度积分节点,当我 运行 我的重置功能时,我想从与该用户相关的忠诚度积分节点中删除所有条目。
我已经设法将所有条目添加到一个名为 'myPoints' 的变量中,方法是将其添加到我的构造函数中:
this.angularfire.auth.subscribe(res => {
if (res != null) {
let userID = res.auth.uid;
this.af.list('/loyaltypoints', {
query: {
orderByChild: 'userId',
equalTo: userID,
}
}).subscribe(response => {
this.myPoints = response;
})
}
})
我现在不知道如何 运行 this.myPoints 变量上的 remove() 函数来删除所有这些条目。
谢谢
更新
所以这似乎对我有用,首先像这样导入: 导入 'rxjs/add/operator/take';
然后此代码删除所有相关的 firebase 条目:
this.angularfire.auth.subscribe(res => {
if (res != null) {
let userID = res.auth.uid;
this.af.list('/loyaltypoints', {
preserveSnapshot: true,
query: {
orderByChild: 'userId',
equalTo: userID,
}
}).take(1).subscribe(response => {
console.log(response)
response.forEach((snapshot) => {
this.af.object('/loyaltypoints/' + snapshot.key).remove();
})
})
}
})
所以您缺少的是获取与每个忠诚度积分条目对应的密钥。
this.angularfire.auth.subscribe(res => {
if (res != null) {
let userID = res.auth.uid;
this.af.list('/loyaltypoints', {
preserveSnapshot: true,
query: {
orderByChild: 'userId',
equalTo: userID,
}
}).take(1).subscribe(snaphots=> {
snapshots.forEach((snapshot) => {
this.af.object('/loyaltypoints/' + snapshot.key).remove();
})
})
}
})
如果您注意到我添加了一个 take(1),因为如果您也在更改您订阅的节点,它会再次触发数据提取。您可能需要稍微尝试一下以确保它是正确的。