flamelink / flamelink-js-sdk

🦊 Official Flamelink JavaScript SDK for both the Firebase Realtime database and Cloud Firestore
https://flamelink.github.io/flamelink-js-sdk
MIT License
43 stars 5 forks source link

Filtering content on relationship data issue #78

Closed shaunnez closed 5 years ago

shaunnez commented 5 years ago

Hello

Assuming I have a collection that has a relationship with another field, I'd assume I can filter it as follows const data = await flamelinkApp.content.get({ schemaKey: "quote", populate: true, filters: [[ "user", "==", "eVFw95jlMpNmsisGzKJWGHOSguf1" ] }); I've tried using ["user", "==", "eVFw95jlMpNmsisGzKJWGHOSguf1"] ["user.id", "==", "eVFw95jlMpNmsisGzKJWGHOSguf1"] ["user", "==", "/fl_users/eVFw95jlMpNmsisGzKJWGHOSguf1"] ["user._fl_meta_.docId", "==", "eVFw95jlMpNmsisGzKJWGHOSguf1"] But nothing works. Help.

If i remove the filters then I can see the data.

jperasmus commented 5 years ago

Hi,

Can you please give me an example of how your data is set up? The filters property will be applied server-side (in Firestore self), so it won't know about the populated data yet - that only happens after the filtering.

shaunnez commented 5 years ago

Quote collection: Title (text) User (select field relational - reference to firebase users)

Just trying to filter by collection based on the authenticated user. I. E. Load authenticated users quotes.

shaunnez commented 5 years ago

I've attempted setting up a rule but that doesn't work either

match /fl_content/{content}/{document=**} {
    allow read: resource.data.user == request.auth.uid;
}
jperasmus commented 5 years ago

Thanks for the example. When using the select-relational field to select a user, that user is stored in your Firestore document as a Document Reference. This means that you can't search/filter on it using a string, but you'll have to use a reference.

Your code would look something like this:

const userId = "eVFw95jlMpNmsisGzKJWGHOSguf1"

const userDocRef = firebase.firestore()
   .collection('fl_users')
   .doc(userId);

const data = await flamelinkApp.content.get({
    schemaKey: "quote", 
    populate: true, 
    filters: [[ "user", "==", userDocRef ]] 
});

Please let me know if that works for you.

shaunnez commented 5 years ago

Yup that works perfectly thanks!

ovkulkarni4 commented 3 years ago

Thanks for the example. When using the select-relational field to select a user, that user is stored in your Firestore document as a Document Reference. This means that you can't search/filter on it using a string, but you'll have to use a reference.

Your code would look something like this:

const userId = "eVFw95jlMpNmsisGzKJWGHOSguf1"

const userDocRef = firebase.firestore()
   .collection('fl_users')
   .doc(userId);

const data = await flamelinkApp.content.get({
  schemaKey: "quote", 
    populate: true, 
  filters: [[ "user", "==", userDocRef ]] 
});

Please let me know if that works for you.

@jperasmus I tried this with "^1.0.0-alpha.34" version. Can you please confirm if this has changed by any means? It is not yet working for me. I can still see null values.