0

Articles I've read/tried before:

I have a Firestore that looks like this enter image description here

  1. The current feature I'm trying to implement is a block list. Whenever you go to the person's profile, you click block, and the person doing the blocking's UID is stored as the doc id and the blocked user's id is added to the "blockedUserId" array
  2. The home page displays all the posts and I've been trying to filter the displayed posts to not include posts from any of the users in that "blockedUserId" array
  3. How do I go about doing this correctly?

Here is the attempted code

query(
      collection(getFirestore(fireApp), "posts"),
      orderBy("uid"),
      where(
        "uid",
        "not-in",
        doc(getFirestore(fireApp), "block", "vXLCRjlhOVW6oFOJvtmML6OolKA2")
      )
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Anteos1
  • 99
  • 1
  • 10

1 Answers1

1

Firestore queries can only filter on values in the document itself, and values you explicitly pass in to the query. Your doc(getFirestore(fireApp), "block", "vXLCRjlhOVW6oFOJvtmML6OolKA2") creates a DocumentReference, so the query returns documents from posts that don't contain that document reference.

What you want to do instead is:

  1. Load the blocked UIDs
  2. Pass them to the query

So in code:

const myRef = doc(getFirestore(fireApp), "block", "vXLCRjlhOVW6oFOJvtmML6OolKA2");
const myDoc = await getDoc(myRef);
const blocked = myDoc.data().blockedUserId;
const q = query(
  collection(getFirestore(fireApp), "posts"),
  orderBy("uid"),
  where(
    "uid",
    "not-in",
    blocked
  )
)
// TODO: call getDocs or onSnapshot on q
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks! This makes sense and I've tried this, but it still says "a non empty array is required for 'not-in' filters" (https://stackoverflow.com/questions/64428495/using-firestore-in-vue-where-doesnt-return-any-results-but-they-exist). Am trying to call the data correctly? – Anteos1 Sep 18 '22 at 20:44
  • So it seems like the `blocked` array is empty then. Did you run in a debugger to verify that? – Frank van Puffelen Sep 19 '22 at 02:59