如何存储名称相同但条目不同的多组数据

时间:2020-03-12 14:52:06

标签: java android firebase google-cloud-firestore

嗨,我有一个用户在我的应用程序中填写的表格,其中包括以下内容: 标题,用户名,密码,网址和注释。 我当前的代码存储一个条目的数据没有问题,但是当我输入一个新条目时,数据将被覆盖。我需要能够存储表单的多个条目。我需要对代码进行哪些更改才能启用此功能?

通过我程序中的其他代码对字符串进行加密,这就是为什么它被try / catch包围的原因。

 public void storePassword() {
        final String title_entry = title.getText().toString().trim();
        final String username_entry = title.getText().toString().trim();
        final String password_entry = title.getText().toString().trim();
        final String webaddress_entry = title.getText().toString().trim();
        final String note_entry = title.getText().toString().trim();



        Map<String, Object> user = new HashMap<>();
        try {
            user.put("title", EncryptDecrypt.encryptString(title_entry, Password));
        } catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        try {
            user.put("username", EncryptDecrypt.encryptString(username_entry, Password));
        } catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        try {
            user.put("password", EncryptDecrypt.encryptString(password_entry, Password));
        } catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        try {
            user.put("webAddress", EncryptDecrypt.encryptString(webaddress_entry, Password));
        } catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        try {
            user.put("note", EncryptDecrypt.encryptString(note_entry, Password));
        } catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        userID = firebaseAuth.getCurrentUser().getUid();
        fDatasebase.collection("Users").document(userID).update(user);

    }

1 个答案:

答案 0 :(得分:1)

如果要为每个用户存储多个表单,则不能为每个表单使用相同的文档ID。相反,您应该使用随机文档ID,并将用户ID作为字段存储在文档中。

Map<String, Object> user = new HashMap<>();
// populate the map as you were

// put the UID in the map
String uid = firebaseAuth.getCurrentUser().getUid();
user.put("uid", uid);

// Add the document to the colleciton with a random ID
fDatasebase.collection("Users").add(user);

要获取用户的所有表单,您只需在uid字段上使用过滤器查询“用户”即可。