如何删除Firebase中的特定节点? [英] how delete a specific node in firebase?

查看:47
本文介绍了如何删除Firebase中的特定节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我连接了firebase并在其中保存了数据.但是我无法删除Firebase中的特定条目.

I hooked up firebase and made the save data in it. But I can't delete a specific entry in firebase.

我尝试通过下面编写的方法删除.但这不起作用.

I tried deleting through method that I wrote below. But it doesn't work.

final FirebaseDatabase _database = FirebaseDatabase.instance;
delData(String phoneNumber) {_database.reference()
.child('phoneNumber')
.remove()
.then((_) {
    print("Delete $phoneNumber successful");
    setState(() {
      // 
    });
  });
}


更新

Frank van Puffelen建议我使用的方法无法正常工作,它删除了具有phoneNumber字段的所有条目,并且我需要删除具有授权用户的电话号码的条目.此外,当在phoneNumber字段中添加新笔记时,该方法会进行侦听并自动将其删除

the method that I was advised by Frank van Puffelen does not work correctly, it removes all entries with the phoneNumber field, and I need to delete the entry with the phone number under which the user is authorized. In addition, the method listens when new notes are added with the phoneNumber field and deletes them automatically

removeNode() {
    FirebaseDatabase.instance.reference()
        .child('customers')
        .orderByChild('phoneNumber')
        .equalTo(phoneNumber)
        .onChildAdded.listen((Event event) {
      FirebaseDatabase.instance.reference()
          .child('customers')
          .child(event.snapshot.key)
          .remove();
    }, onError: (Object o) {
      final DatabaseError error = o;
      print('Error: ${error.code} ${error.message}');
    });
  }````
// This is how is i add data to DB     
   ````void handleSubmit() {
      final FormState form = formKey.currentState;
      print(userPhone);
      if (form.validate()) {
        form.save();
        form.reset();
        itemRef.push().set(item.toJson());}````

也许我应该使用Firestore?

Maybe I should use firestore?

推荐答案

要删除节点,您需要知道该节点的完整路径.如果您不知道完整的路径,则可以使用查询来确定它.

In order to delete a node, you need to know the complete path to that node. If you don't know that complete path, you can use a query to determine it.

因此,在您的情况下,如果知道要删除的节点的电话号码,则可以查询具有该电话号码的所有节点(因为可能有多个),然后将其删除.

So in your case, if you know the phone number of the node you want to delete, you can query for all nodes with that phone number (since there may be multiple) and then delete them.

此代码类似于:

FirebaseDatabase.instance.reference()
  .child('customers')
  .orderByChild('phoneNumber')
  .equalTo('+79644054946')
  .onChildAdded.listen((Event event) {
    FirebaseDatabase.instance.reference()
      .child('customers')        
      .child(event.snapshot.key)
      .remove();
  }, onError: (Object o) {
    final DatabaseError error = o;
    print('Error: ${error.code} ${error.message}');
  });


更新(20200221):您还可以使用 once()侦听器,并使用以下命令遍历其子节点:


Update (20200221): You can also use a once() listener and loop over its child nodes with:

FirebaseDatabase.instance.reference()
  .child('customers')
  .orderByChild('phoneNumber')
  .equalTo('+79644054946')
  .once()
  .then((DataSnapshot snapshot) {
    Map<dynamic, dynamic> children = snapshot.value;
    children.forEach((key, value) {
      FirebaseDatabase.instance.reference()
        .child('customers')        
        .child(key)
        .remove();
    })
  });

这篇关于如何删除Firebase中的特定节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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