Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding a map to an array on Firestore Android

I’m having a little trouble getting my data to save to a Firestore database. Each document in my "meetings" collection has a list of users (maps), but I cannot seem to add anything or create a new document. I read the documentation, and it said to use FieldValue.arrayUnion(), which I tried:

    private fun addUserToMeeting(user: User) {
        val meetingRef = database.collection("meetings").document(meetingID.toString())

        val userData = hashMapOf(
            "email" to user.email,
            "latitude" to user.latitude,
            "longitude" to user.longitude,
            "token" to user.token,
            "username" to user.username
        )

        meetingRef.update("users", FieldValue.arrayUnion(userData))
    }

This is called anytime an "add user to meeting" button is clicked.

Here’s a picture of my schema: enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Has anyone got some suggestions on what I might be doing wrong?

>Solution :

If the document doesn’t exist yet, you can’t use update to create it. As its name implies, update can only be used to update an existing document.

To create a new document at a location you control, use set:

meetingRef.set(hashMapOf("users" to FieldValue.arrayUnion(userData)))

Also see the Firebase documentation on setting a document.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading