I am trying to obtain some docs from Firestore with a query that I thought I wouldn't have issues but I am not achieving it. I have some data in my state which looks like:
const favorites = [ "1_Test1", "2_Test2" ]
const collectionRef = collection( FirebaseDB, `users/${ uid }/scores` );
const q = query(collectionRef, where('id','in',`${favorites}`));
const [scores] = useCollectionData( q, collectionRef );
console.log(scores)
But this is giving me an empty array. I have tried the following code just to test if it was arriving to Firestore:
const favorites = [ "1_Test1", "2_Test2" ]
const collectionRef = collection( FirebaseDB, `users/${ uid }/scores` );
const q = query(collectionRef, where('id','==','1_Test1'));
const [scores] = useCollectionData( q, collectionRef );
console.log(scores)
And that worked just fine. It gaves me that particular document. So I am doing something wrong in the first query. I have tried several things like removing brackets but I don't get which is the issue.
CodePudding user response:
The problem is that you use string interpolation in `${favorites}`
. This converts favorites
to a string, which Firestore then uses for the comparison.
To fix the problem, pass the array like this:
const q = query(collectionRef, where('id', 'in', favorites));