My console log is 2 differents id : sender.id and otherparticipants[0].id
I need to query my first document with sender.id OR otherParticipants (because it could be one or another one)
I'm trying to do that with an operator OR but firebase seems doesn't recognize my second value.
Do you know how I can have the same result in a good way?
.then(() => {
console.log('participant', otherParticipants[0].id, sender.id);
firebase
.firestore()
.collectionGroup('favoris')
.where('id', '==', sender.id || otherParticipants[0].id)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log('data2', doc.data());
firebase
.firestore()
.collectionGroup('favoris')
.where('idUser', '==', sender.id || otherParticipants[0].id)
.get()
.then((querySnapshot) => {
console.log(querySnapshot.size);
querySnapshot.forEach((doc) => {
console.log('dataFinal', doc.data());
socialFeedsRef
.doc(sender.id)
.collection('chat_feed')
.doc(channel.id)
.set(
{
favoris: true,
},
{ merge: true },
);
});
});
});
});
});
That's not how you should create an OR query in Firestore:
.where('id', '==', sender.id || otherParticipants[0].id)
In this line, the || operator doesn't help.
You should create an array containing the IDs of the users who participate in the chat, say participantIDs: ["yourSenderUID", "yourOtherUid"].
Then, instead of the above call, add the following one:
.where("participantIDs", "array-contains-any", [sender.id, otherParticipants[0].id])
When you execute such a query, you'll get a warning message that sounds like this:
FAILED_PRECONDITION: The query requires a COLLECTION_GROUP_ASC index for collection favoris and field id. You can create it here: ...
Meaning that an index is required. You can simply click on that URL, or copy and paste the URL into a web browser and your index will be created automatically for you.