How would you store multiple sets of data with the same name but different entrys firestore

dawnofwar :

Hi i have a form the user fills out within my app which includes the following: title, username, password, webAddress and note. My current code stores the data for one entry no problem but when i go to enter a new entry the data gets overwritten. I need to be able to store multiple entrys of the form. What would i need to change on my code to enable this?

The strings are encrypted via other code in my program thats why its surrounded by a 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);

    }
Doug Stevenson :

If you want store multiple forms per user, you can't use the same document ID for each form. You should instead use a random document ID, and store the user ID as a field in the document.

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);

To get all the forms for the user, you can just query "Users" with a filter on the uid field.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=215947&siteId=1