问题
I am trying to delete comments of a particular thread orderby some key(sid) value.
eComments
0b4080bb4e686f003aaa340f8ed2e2a6
cid: "0b4080bb4e686f003aaa340f8ed2e2a6"
comment: "Prison Break revolves around two brothers: one ..."
createdAt: 1487250871623
rating: 0
sid: "c088239a29827946f932f73c9a1d495a"
uid: "SFmtrI0ta5PsqYkgqZuJo2"
updatedAt: 1487250871623
4bde9de83ac2bb6d06df9876c2294483addclose
cid: "4bde9de83ac2bb6d06df9876c2294483"
comment: "arrives at the jail, he meets the prison denize..."
createdAt: 1487251466761
rating: 0
sid: "e8c2d3c2aaf877fcdf0c103229645981"
uid: "SFmtrI0ta5PsqYkgqZuJo2E"
updatedAt: 1487251466761
Let say I wanted to delete sid: "c088239a29827946f932f73c9a1d495a"
I tried this, but unfortunately it deletes all the eComments database
const commentList = this.af.database.list('/eComments', {
query: {
orderByChild: 'sid',
equalTo: sid
}
});
commentList.remove();
Anyway I can delete by key value?
回答1:
This is one way:
const commentList = this.af.database.list('/eComments', {
preserveSnapshot: true,
query: {
orderByChild: 'sid',
equalTo: sid
}
});
commentList.subscribe(snapshots=>{
snapshots.forEach(snapshot => {
snapshot.ref.remove();
});
})
I am not an AngularFire2 expert, so there might be easier/more idiomatic ways.
来源:https://stackoverflow.com/questions/42275384/how-to-angularfire2-remove-by-some-key-value