为什么删除Firebase中的数据removeValue()不起作用?

时间:2017-04-03 19:48:17

标签: ios swift firebase firebase-realtime-database

我希望当用户已经按下downvote时,下次他们按下向下投票按钮时,他们将从downvote字典中删除。

enter image description here

Firebase.getDownvote(selectedPostTimeStamp: postItem.timeStamp) { (error, downvoteFinalDictionary) in
        let keyExists = downvoteFinalDictionary?[Current.user.userID!] != nil

        // if the current user exists in the downvote dictionary
        if keyExists {
            print("Current UID exist")
            // remove the current user from the downvote dictionary
            self.databaseRef.child("channels").child(Current.user.channel).child("posts").child("post").child(Current.user.userID!).removeValue()

        } else {
             // uid doesn't exist   
        }

我也试过这个:(

self.databaseRef.child("channels").child(Current.user.channel).child("posts").child("post").child(Current.user.userID!).removeValue(completionBlock: { (error, databaseReference) in
                print("\n\n\n DATA IS SUPPOSED TO BE GONE!!!!!")
            })

2 个答案:

答案 0 :(得分:1)

您的结构看起来与Firebase查询不匹配。你的结构是渠道>频道>帖子>发布>日期>用户身份。但是您尝试的代码是渠道>频道>帖子>发布>用户身份。 Firebase无法在该路径上找到userId,因此不会发生任何事情。

尝试以下代码让您更接近:

self.databaseRef.child("channels")
                .child(Current.user.channel)
                .child("posts/post")
                .observeSingleEvent(type: .value, with: { (snapshot) in


        })

如果有必要,我稍后会编辑此代码,以便您进一步了解。我现在不在我的Mac上。

答案 1 :(得分:0)

我的不好,我跳过2个孩子:postIdentifier and downvotes :)。

相关问题