keremistan
keremistan

Reputation: 444

Querying based on doc id in firestore cloud functions doesn't return any result

I'm trying to query Firestore with a onDelete trigger. I'm getting the deleted doc's id.

This is a user doc and I want to fetch the 'friends' of that deleted user. Friends is an array in a user document. For hours, I have been trying many solutions I've seen to get those friends but none of them worked.

What I have tried:

These friends array contain the other users' ids as doc reference.

Interestingly, when I put those fields as a normal doc property, the query still didn't return anything. I suspect something about the firestore versions and not properly queryig. Because the stuff I've tried should have been working in some versions of FS.

I'm running the queries in local emulator.

I must be overseeing something very obvious. Thanks in advance.

Upvotes: 1

Views: 43

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50930

If friends is an array of DocumentReference then you need that reference when querying and not the document ID.

const friendsOfDeletedUser = await fs
  .collection("users")
  .where(
    "friends",
    "array-contains",
    fs.collection("users").doc(ctx.params.userId) // no .id or .path
  )
  .get();

This will return all documents where friends array contains reference to deleted user.

Upvotes: 1

Related Questions