如何一步删除列表查询结果?
How can I delete result of list query in one step?
我试过删除 operations
,其中产品 ID 等于 myProdID
。
它删除了整个操作分支,而不仅仅是等于查询结果的分支。
this.af.database.list('operations', {
query: {
orderByChild: 'products/productID',
equalTo: myProdID
}
}).remove();
我应该使用什么来在一行代码中完成它而不是 运行 for 循环来删除每个项目? .map ?
您可以像这样执行单个更新:
ref.update({
'/operations/products/foo': null,
'/operations/products/bar': null
});
这将从 ref/operations/products
中批量删除 foo 和 bar 子项,同时保持所有其他子项不变。
但我想您仍然需要进行一些循环来找出要更新的路径。
这不仅仅是一行代码,但您可以这样做:
deleteOperations(productID: any): Observable<any> {
return this.af.database.list('operations', {
query: {
orderByChild: 'products/productID',
equalTo: productID
}
})
// AngularFire2 list/object observables don't complete - they re-emit if
// the database changes - so use the first operator to ensure it completes
// and ignores subsequent database changes.
.first()
// Use Array.prototype.reduce to create an object containing the keys to
// be removed and use the FirebaseObjectObservable's update method to
// remove them.
.mergeMap((ops) => this.af.database.object('operations').update(
ops.reduce((acc, op) => { acc[op.$key] = null; return acc; }, {})
));
}
上述函数将 return 一个可观察对象,删除将在调用者订阅它时执行。
如果您希望函数 return 有一个承诺,您可以这样做:
deleteOperations(productID: any): Promise<any> {
return this.af.database.list('operations', {
query: {
orderByChild: 'products/productID',
equalTo: productID
}
})
// AngularFire2 list/object observables don't complete - they re-emit if
// the database changes - so use the first operator to ensure it completes
// and ignores subsequent database changes.
.first()
// Convert the observable to a promise when that will resolve when the
// observable completes.
.toPromise()
// Use Array.prototype.reduce to create an object containing the keys to
// be removed and use the FirebaseObjectObservable's update method to
// remove them.
.then((ops) => this.af.database.object('operations').update(
ops.reduce((acc, op) => { acc[op.$key] = null; return acc; }, {})
));
}
我试过删除 operations
,其中产品 ID 等于 myProdID
。
它删除了整个操作分支,而不仅仅是等于查询结果的分支。
this.af.database.list('operations', {
query: {
orderByChild: 'products/productID',
equalTo: myProdID
}
}).remove();
我应该使用什么来在一行代码中完成它而不是 运行 for 循环来删除每个项目? .map ?
您可以像这样执行单个更新:
ref.update({
'/operations/products/foo': null,
'/operations/products/bar': null
});
这将从 ref/operations/products
中批量删除 foo 和 bar 子项,同时保持所有其他子项不变。
但我想您仍然需要进行一些循环来找出要更新的路径。
这不仅仅是一行代码,但您可以这样做:
deleteOperations(productID: any): Observable<any> {
return this.af.database.list('operations', {
query: {
orderByChild: 'products/productID',
equalTo: productID
}
})
// AngularFire2 list/object observables don't complete - they re-emit if
// the database changes - so use the first operator to ensure it completes
// and ignores subsequent database changes.
.first()
// Use Array.prototype.reduce to create an object containing the keys to
// be removed and use the FirebaseObjectObservable's update method to
// remove them.
.mergeMap((ops) => this.af.database.object('operations').update(
ops.reduce((acc, op) => { acc[op.$key] = null; return acc; }, {})
));
}
上述函数将 return 一个可观察对象,删除将在调用者订阅它时执行。
如果您希望函数 return 有一个承诺,您可以这样做:
deleteOperations(productID: any): Promise<any> {
return this.af.database.list('operations', {
query: {
orderByChild: 'products/productID',
equalTo: productID
}
})
// AngularFire2 list/object observables don't complete - they re-emit if
// the database changes - so use the first operator to ensure it completes
// and ignores subsequent database changes.
.first()
// Convert the observable to a promise when that will resolve when the
// observable completes.
.toPromise()
// Use Array.prototype.reduce to create an object containing the keys to
// be removed and use the FirebaseObjectObservable's update method to
// remove them.
.then((ops) => this.af.database.object('operations').update(
ops.reduce((acc, op) => { acc[op.$key] = null; return acc; }, {})
));
}