我想删除当前用户或当前uid的帖子。发布的用户的uid由String:uid在JSON中指定。所以我在快照中搜索uid,如果它存在,我删除该uid的父节点。问题是所有帖子都被删除而不是只存储当前uid的帖子。
JSON树
Posts
-LBzjsl0Yfwdyh9xg6D2 (this is random uid)
photoUrl: "https://firebasestorage.googleapis.com/v0/b/blo..."
uid: "BBPHwd0RRibXFCvHcXA2aKbHoTm1" (this is uid of the current user)
代码:
let user = Auth.auth().currentUser?.uid
databaseRef.child("Posts").queryOrdered(byChild: "uid").queryEqual(toValue: user).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
//find out parent of current post
let parent = snapshot.ref.parent!.key
let ref = Database.database().reference()
print (parent)
func remove(child: String) {
ref.removeValue { error, _ in
}
}
remove(child: parent)