无法删除Firebase节点 [英] Unable to remove Firebase node

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

问题描述

我对此不知所措.我无法从Web应用程序或通过Firebase控制台删除Firebase中的节点.通过set(null)remove()删除/courses/{courseId}/members/{uid}节点后,将立即添加用户信息.

I'm at a loss on this one. I'm unable to delete a node in Firebase either from the web app or through the Firebase console. When the /courses/{courseId}/members/{uid} node is removed, either through set(null) or remove(), the user's information is added immediately.

我确实有两个更新seats节点的云函数,因此我们可以跟踪空间,但是这些都没有指向../{uid}端点.我还检查了我的Web应用程序代码,以确保没有任何on_delete事件写入该树. 有什么线索吗?

I do have two cloud functions updating the seats node so we can keep track of space, but neither of those point at the ../{uid} endpoint. I've also gone through my web app code to make sure there were no on_delete events writing to the tree. Any clues as to what's happening?

更新

在提交树中向后移动后,我能够确认它是下面的云函数,不允许从树中删除.因此,我的问题现在变成了,此功能中的什么导致了该行为?我看不到任何会重写数据的东西.我的数据库结构如下.

After going backward in my commit tree, I was able to confirm that it was the below cloud function disallowing deletes from the tree. So, my question now becomes, what in this function causing the behavior? I can't see anything that would re-write data. My database structure is below.

/课程参考

courses: {
    someStringId1234: {
        members: {
            uid12345: {
                code: 'someString',
                email: 'some@email.com'
            },
            uid67890: { ... }
        },
        seats: 10
      },
      { ... }
}

此云功能监视uid项目的更改.它获取父节点(members),然后更新seats中的计数.由于某种原因,这是在重写先前删除的密钥并将其设置为0.

This cloud function watches for changes to the uid item. It grabs the parent node (members) and then updates the count in seats. For some reason, this is re-writing the previously deleted key and setting it to 0.

countRegistration,firebase云功能

exports.countRegistrations = functions.database.ref('/courses/{courseId}/members/{uid}').onWrite(
  (change) => {
      // get the `members` node for the item
      const collectionRef = change.after.ref.parent;
      console.log('collectionRef', collectionRef)
      // get the `seats` key for updating
      const countRef = collectionRef.parent.child('seats');
      console.log('countRef', countRef)

      let increment;
      // If the ID is there after but not before, remove one seat
      if (change.after.exists() && !change.before.exists()) {
        increment = -1;
        // if the ID is not there after, but there before, add one seat
      } else if (!change.after.exists() && change.before.exists()) {
        increment = 1;
      } else {
        // Nothing to change
        return null;
      }

      // Return the promise from countRef.transaction() so the function
      // waits for this async event to complete before it exits.
      return countRef.transaction((current) => {
        console.log('Line 38', current)    // debugging, confirms the correct refs are grabbed by the function
        return (current || 0) + increment;
      }).then(() => {
        return
      });
    });

只是为了好玩,这是当我尝试直接在控制台中删除节点时发生的事情.数据库规则允许您在登录后进行写操作.

Just for fun, here's what happens when I try to delete the node directly in the console. The database rules allow writing if you're logged in.

推荐答案

对于这种情况,我正在使用处理程序将我的活动刷新延迟2-3秒,如果有人遇到此错误,请确保:-

For this scenario, i am using handler to delay the refresh of my activity with 2-3 seconds, If anyone is getting this error then make sure:-

  1. 请勿刷新活动或显示(重新加载数据库)

  1. Do not make the refresh of an activity or display (reloading the database)

使用具有2-3秒延迟的处理程序,然后登录的用户无法自动重新制作该节点.

Use handler with 2-3 seconds delay, then the logged in user can't auto remake the node.

我以前如何删除节点:-

How i used to delete the node:-

 myViewHolder.deleteicon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    String childvalue = list.get(i).getPosition();
                    Log.d(TAG, "onClick: "+ childvalue);
                    FirebaseDatabase.getInstance().getReference()
                            .child("Submission/Rules").child(categorynametext).child(user.getUid()).child(childvalue).removeValue()
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {
                                        Toast.makeText(context, "You have removed successfully", Toast.LENGTH_LONG).show();


                                        try
                                        {
                                            Handler handler = new Handler();
                                            handler.postDelayed(new Runnable() {
                                                @Override
                                                public void run() {

                                   ///Here i am refreshing the activity
                                                    ((Activity)context).finish();
                                                    ((Activity)context).overridePendingTransition( 0, 0);
                                                    ((Activity)context).startActivity(((Activity) context).getIntent());
                                                    ((Activity)context).overridePendingTransition( 0, 0);
                                                }
                                            }, 3000);
                                        }
                                        catch (Exception e){

                                            e.printStackTrace();
                                        }



                                    } else {
                                        //enter msg or enter your code which you want to show in case of value is not remove properly or removed failed.
                                        Toast.makeText(context, "There is an error while removing the rule, Please try again later", Toast.LENGTH_LONG).show();
                                    }
                                }
                            });




                }
            });    

更新:

在进行更多工作之后,我发现由于使用addValueEventListener<,我也遇到了相同的错误.在这种情况下,它是在删除或添加数据库中的东西时自动添加数据,如果您使用addListenerForSingleValueEvent,则在添加或删除数据时不会有任何问题.

After working more in this, i found i was also getting the same error, because of using addValueEventListener< in this case it is auto adding the data while deleting or adding something in the database and if you use addListenerForSingleValueEvent, then there will not any problem while adding or deleting the data.

最终结论:

addValueEventListener(如果您希望每次发生任何添加或删除操作时刷新页面

addValueEventListener (If you want to refresh the page everytime any add or deletion occurs

addListenerForSingleValueEvent(如果您不想在添加或删除内容时刷新页面)

addListenerForSingleValueEvent(If you don't want to refresh the page while adding or deleting something)

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

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