query.ref.removeValue删除整个孩子,而不是一个节点 [英] query.ref.removeValue removes the entire child instead of just a node

查看:127
本文介绍了query.ref.removeValue删除整个孩子,而不是一个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过查询,然后调用 query.ref.removeValue()从数据库中删除一个特定的节点,虽然每次运行这段代码删除整个孩子,而不是只有节点。我的代码看起来像这样...



$ p $ let query =(ref?.child(Groups)。child(Martins ).child(Messages)。queryEqualToValue(selectedValue))

query!.ref.removeValue()

我的数据库看起来像这样...


感谢所有的帮助,我真的很感激。

解决方案

查询引用指向查询定义的位置,而不是匹配的元素。所以 query!.ref.removeValue()会移除整个位置。



删除单个项目查询匹配,你需要附加一个监听器到查询并迭代子节点:

  let query = ref ?. child(Groups)。child(Martins)。child(Messages)
.queryOrderedByKey(Item)。queryEqualToValue(selectedValue)

query.observeEventType(.ChildAdded ,withBlock:{(snapshot) - >无效
snapshot.ref.removeValue()
})


I am attempting to remove one specific node from my database by querying and then calling the query.ref.removeValue() though every time this piece of code runs it deletes the entire child instead of just the node. My code looks like this...

let query = (ref?.child("Groups").child("Martins").child("Messages").queryEqualToValue(selectedValue))

query!.ref.removeValue()

My database is looking like this...

Thanks for all the help, I really appreciate it.

解决方案

The query reference points to the location that the query is defined on, not to the elements that it matches. So query!.ref.removeValue() will remove the entire location.

To remove the individual items that the query matches, you'll need to attach a listener to the query and iterate over the children:

let query = ref?.child("Groups").child("Martins").child("Messages")
                .queryOrderedByKey("Item").queryEqualToValue(selectedValue)

query.observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in
  snapshot.ref.removeValue()
})

这篇关于query.ref.removeValue删除整个孩子,而不是一个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆