0

I have an app where I want to check if there is a contact with a certain uid.

The problem: The uid I want to check is nested in a map that is in an array.

The Firestore document looks like this:

enter image description here

I thought so far something like this:

  var data = await FirebaseFirestore.instance
    .collection("user_contacts")
    .doc(FirebaseAuth.instance.currentUser.uid)
    .get();

  if(data.data()["contacts"].contains({"uid": myUid, "date": myDate})){
    ...
  }

But I don't have the date. Is there any other way?

Thanks for help!

1 Answer 1

1

I would personally recommend that you try and stay away from arrays within your documents especially if they are going to grow to hundreds of names long. If not careful you could be pulling in unnecessary data on every call.

If you were to change your implementation I would recommend creating a subcollection.

With your current implementation you could use this function:

 const exists= (arr, val) => {
   for(let i=0; i<arr.length; i++){
        return arr[i]['uid'] == val;
   };
}

where arr is data.data()["contacts"] and val is your desired uid

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

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.