1

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

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

2
  • What isn't working about the code? It might be easiest to explain if you show a "before" and "after" screenshot of the document when you code has run. Commented Nov 13, 2021 at 23:23
  • @FrankvanPuffelen It's not doing anything at all. When I run it (by clicking), there's no meeting document or anything. It's as if no updates have happened whatsoever. The "meeting" under the "exampleUUIDForMeeting" was created just to see if I was connected, which is a little shrewd, but it worked in a pinch; it doesn't contain any valuable information. Commented Nov 13, 2021 at 23:30

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Whoops. That definitely was it. facepalm I should have caught that for sure!
I was trying add array from application side and tried many thing. Finally found solution here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.