2

I need to write a simple collection where documents only contain an array list of strings.

usersBlocked
    -blockerID
         - usersBlockedList
               -userID1
               -....
               -userIDN

At this moment I'm able to update the list if the document already exist in this way: (for testing porpouses I wrote it by hand).

val reference = Firebase.firestore
            .collection("usersBlocked")
            .document(userIDBlocker)

reference.update("usersBlocked", FieldValue.arrayUnion(userIDToBlock))
            .addOnSuccessListener { Logger.d("Blocked") }
            .addOnFailureListener { Logger.d(it.localizedMessage) }

As I said, if the document exists, the update is correct, but when there is no document... I'm getting this error:

NOT_FOUND: No document to update: projects/xxxxxx/databases/(default)/documents/usersBlocked/2oLOYbG2z9gRC5VugchEP7ZPpy53

I assume that obviously not exist, but I don't know how to write Arrays in a document.

I saw how to save Maps, but is not what I need.

DB Structure:

enter image description here

2
  • Uploaded a image of the db @Ashish Commented Oct 11, 2019 at 12:31
  • 1
    please check my answer if any queries just comment down Commented Oct 11, 2019 at 12:35

2 Answers 2

1

First you have to confirm that there is any document available with your current userid. Put condition like i have put. It will check whether id is available or not. If id is not available. Then it will generate new document at your usersBlocked with uid and store array.

val reference = Firebase.firestore
    .collection("usersBlocked")
    .document(userIDBlocker).get()
        .addOnSuccessListener { documentSnapshot ->
            if (documentSnapshot.exists()) {
                reference.update("usersBlocked", FieldValue.arrayUnion(userIDToBlock))
            } else {
                val userdetail = HashMap<String, Any>()
                var usersBlockedList = arrayListOf<String>()
                usersBlockedList.add(userIDToBlock)
                userdetail["usersBlocked"] = usersBlockedList
                Firebase.firestore.collection("usersBlocked").document(userIDBlocker).set(userdetail)
                        .addOnSuccessListener { success ->

                        }
                        .addOnFailureListener { exception ->
                            Log.e("Data Failed", "To added because ${exception}")
                        }

            }
        }
        .addOnFailureListener { exception ->
        Log.e("Exception", "${exception}")
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Worked perfect. The point I missed is where you created the hashmap to add the "param". Thanks! @Ashish
0

Replace .update(data) with .set(data, SetOptions.merge()) - this will create the document is it's missing, and update it otherwise.

Data would be a map:

val data = mapOf(
    "usersBlocked" to FieldValue.arrayUnion(userIDToBlock)
)

Docs: https://firebase.google.com/docs/firestore/manage-data/add-data#set_a_document

Comments

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.