简体   繁体   English

Firebase数据库使用setValue()覆盖以前的值

[英]Firebase Database overwrites previous value with setValue()

So I have a Database in Firebase. 所以我在Firebase中有一个数据库。

This code is in onCreate(): 这段代码在onCreate()中:

DatabaseReference mDatabaseRefUser =FirebaseDatabase.getInstance().getReference("Users");

This code is in another method that gets called onClick: 这段代码在另一个称为onClick的方法中:

mDatabaseRefUser.child("Chat").child(mAuth.getCurrentUser().getUid())
            .child(userID).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
            mAuth.getCurrentUser().getEmail()));

mDatabaseRefUser.child("Chat").child(userID)
            .child(mAuth.getCurrentUser().getUid()).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
            userEmail));

What im trying to do is to add a new value without deleting the previous/existing value everytime the method gets called. 我试图做的是每次调用该方法时都添加一个新值,而不删除先前/现有的值。

For the moment everytime I click and start the method, the previous/existing value gets removed and the new one gets added. 每次单击并启动该方法时,先前/现有的值都会被删除,而新值将被添加。 But I want neither to be deleted. 但是我都不希望被删除。 I want this the new value to be added aswell. 我希望这个新值也能被添加。 I have tried with getRef and push() but my data still gets overwritten. 我已经尝试过使用getRef和push(),但是我的数据仍然被覆盖。 What am I doing wrong here? 我在这里做错了什么?

This is my JSON 1st time I call the method: 这是我第一次调用JSON方法时:

"Users" : {
"Chat" : {
  "Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
    "xfqAFlRpbUZRlZb76svN5FUtbU93" : {
      "-LCvIA_9L1Y9rjkm96Aj" : {
        "Messages" : {
      "-LCvqDidnNqQ8ALZhDud" : {
        "messageText" : "Very good",
        "messageTime" : 1526791230552,
        "messageUser" : "mama@mama.com"
      }
    },
    "chatAgainstUserEmail" : "mama2@mama.com",
    "chatAgainstUserID" : "xfqAFlRpbUZRlZb76svN5FUtbU93",
    "uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
    "userEmail" : "mama@mama.com",
    "userID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2"
  }
}

},
  "xfqAFlRpbUZRlZb76svN5FUtbU93" : {
"Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
  "-LCvIA_9L1Y9rjkm96Aj" : {
    "Messages" : {
      "-LCvqDigI8ZwHDYuPhw9" : {
        "messageText" : "Very good",
        "messageTime" : 1526791230555,
        "messageUser" : "mama2@mama.com"
      }
    },
    "chatAgainstUserEmail" : "mama@mama.com",
    "chatAgainstUserID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2",
    "uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
    "userEmail" : "mama2@mama.com",
    "userID" : "xfqAFlRpbUZRlZb76svN5FUtbU93"
      }
    }
  }
}

This is my JSON when I call it for the 2nd time 当我第二次调用它时,这是我的JSON

"Users" : {
"Chat" : {
  "Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
    "xfqAFlRpbUZRlZb76svN5FUtbU93" : {
      "-LCvIA_9L1Y9rjkm96Aj" : {
        "Messages" : {
      "-LCvr0zoHOPxsKOVJzeu" : {
        "messageText" : "Very good 2nd call",
        "messageTime" : 1526791440547,
        "messageUser" : "mama@mama.com"
      }
        },
    "chatAgainstUserEmail" : "mama2@mama.com",
    "chatAgainstUserID" : "xfqAFlRpbUZRlZb76svN5FUtbU93",
    "uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
    "userEmail" : "mama@mama.com",
    "userID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2"
  }
}
  },
  "xfqAFlRpbUZRlZb76svN5FUtbU93" : {
"Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
  "-LCvIA_9L1Y9rjkm96Aj" : {
    "Messages" : {
      "-LCvr0zrh8wQ2WurUezs" : {
        "messageText" : "Very good 2nd call",
        "messageTime" : 1526791440550,
        "messageUser" : "mama2@mama.com"
      }
    },
    "chatAgainstUserEmail" : "mama@mama.com",
    "chatAgainstUserID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2",
    "uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
    "userEmail" : "mama2@mama.com",
    "userID" : "xfqAFlRpbUZRlZb76svN5FUtbU93"
      }
    }
  }
}

I have also tried with getRef().push() , but that didn't work either. 我也尝试过使用getRef().push() ,但这也不起作用。 I have also tried without . 我也试过了。 getRef() and without .push() , didnt work either. getRef()和没有.push()任何一个都不起作用。

To update a value instead of overriding it, you should use updateChildren() method. 要更新值而不是覆盖它,应使用updateChildren()方法。

You are overriding it because every node in a Firebase database is a Map and in the case of Map , it replaces the old value with the new one. 之所以要覆盖它,是因为Firebase数据库中的每个节点都是Map ,对于Map ,它将新值替换为旧值。

To update data you should go for such kind of code instead of using child.setValue() as its name suggests it will directly replace old values with new values. 要更新数据,您应该使用此类代码,而不要使用child.setValue() ,顾名思义,它将直接用新值替换旧值。 But If you want to change a specific value then you should go for something as below. 但是,如果要更改特定值,则应进行以下操作。 Give it a try mate! 给它一个尝试伴侣!

HashMap<String, Object> params = new HashMap<>();
params.put("yourKey", your value);   
yourDatabaseChildReference.updateChildren(params);

My only solution to this issue was to make a new Database, 我对此问题的唯一解决方案是创建一个新的数据库,

I added this: 我添加了这个:

mDatabaseRefUser.child("Chatting").child(mAuth.getCurrentUser().getUid())
                .child(userID).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
                mAuth.getCurrentUser().getEmail()));

        mDatabaseRefUser.child("Chatting").child(userID)
                .child(mAuth.getCurrentUser().getUid()).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
                userEmail));

and removed the old one 并删除旧的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase数据库用setValue覆盖 - Firebase Database overwrites with setValue Firebase 数据库中的 setValue() 方法不起作用。 值未添加到数据库中 - setValue() method in Firebase Database not working. Value is not getting added to the database 对于循环映射覆盖先前的值 - For Loop Map Overwrites Previous Value 如何从setValue Firebase数据库退出? - How to exit from setValue Firebase Database? 单击AlertDialog在Firebase数据库上设置值时出错 - Error when clicking AlertDialog to setValue at Firebase database Firebase 实时数据库 Hashmap 覆盖数据 - Firebase realtime Database Hashmap overwrites data Java:使用mapDB,在数据库中写入会覆盖以前的条目 - Java: Using mapDB, writing in Database overwrites previous entry Firebase - 使用setValue(Object)时,不要将特定变量存储在数据库中; - Firebase — Do not store specific variable in database when using setValue(Object); Firebase 实时数据库在添加新数据的同时覆盖旧数据 - Firebase Realtime Database overwrites old data while adding new 关于 firebase 存储和 firebase 防火墙工作,我无法通过带有 setValue 的片段在 Firebase 数据库中添加数据 - I cant add data in Firebase Database through a Fragment with setValue, regarding firebase storage and firebase firestore working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM