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

标签 ios swift firebase firebase-realtime-database

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

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!!!!!")
            })

最佳答案

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

试试下面的代码让你更接近:

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


        })

如有必要,我稍后会编辑此代码以帮助您进一步了解。我现在不在我的 Mac 旁边。

关于ios - 为什么在 Firebase removeValue() 中删除数据不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43193090/

相关文章:

ios - 带有 mask 层的 UIVisualEffectView

ios - automaticallyAdjustsScrollViewInsets 不工作

ios - UIBarButtonItemStyleBordered 已弃用;我该怎么办呢?

ios - Swift:在iOS项目中使用Swift是否必须在ipa文件中打包libswiftCore.dylib?

android - 如何根据日期(DAY)从 firebase 获取特定的子值

javascript - 滚动浏览 HTML <select> 列表时 iPad 未检测到触摸

Swift - AVAudioPlayer 无法正常工作

ios - 加载仅在按钮功能后显示的动画(Swift)

Firebase:403 PERMISSION_DENIED(FirebaseError:安装):更新 SDK 后请求被阻止 (FirebaseInstallationsService)

javascript - OnCreate 函数未触发,但 OnWrite 函数触发