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

Using subscribe with fields option returns nothing. #107

Closed erik-slack closed 4 years ago

erik-slack commented 4 years ago

I'm doing this:

      this.accountsRef = this.flamelinkService.getApp().content
        .subscribe({
          schemaKey: 'accounts',
          fields: [
            'email',
            'access'
          ],
          filters: [
            ['email', '==', response.email]
          ],
          callback: (error, data) => {
            if (error) {
              return console.error(error);
            }

            console.log('data', data);

            // this.accountsFacade.loadAccounts(processedData);
          }
        });

And every time data is coming back as empty array. If I remove the fields it comes back as normal. This doesn't happen when I use subscribe with a schema of type Single. It only happens for Collection type schemas. I expect that it should return the accounts that have email and access properties only when I do this.

Version: "flamelink": "^1.0.0-alpha.26",

erik-slack commented 4 years ago

Populate also doesn't seem to work!

this.settingsRef = this.flamelinkService.getApp().content
        .subscribe({
          schemaKey: 'settings',
          fields: [
            'askTheExpertUrl',
            'facebookPrivateUrl',
            'facebookPublicUrl',
            'headerLogo',
            'homeTiles',
            'needAnythingText',
            'startHereText',
            'contactEmail',
            'contactPhone',
            'contactFacebook'
          ],
          populate: [
            {
              field: 'homeTiles',
              populate: ['group', 'image']
            },
            {
              field: 'headerLogo'
            }
          ],
          callback: (error, data) => {
            if (error) {
              return console.error(error);
            }
            console.log('data', data);
            console.log('homeTiles', data.homeTiles);
            const processedData = {
              ...data,
              homeTiles: data.homeTiles.sort((a, b) => a.weight - b.weight)
            };

            this.settingsFacade.loadSettings(processedData);
          }
        });

It just returns as normal without populating those fields

jperasmus commented 4 years ago

Hi, thanks for reporting this. Can you please show me the data structure of the content being returned if you don't include the filters property?

erik-slack commented 4 years ago

It still returns empty object if I don't include filters. Sorry I had to edit my first post because I had said filters when I actually meant--in bold--fields. Filters works fine. Fields doesn't. Also the populate issue is of much higher priority to me than the fields issue. I would love to get the image url back with the call, instead of having to make separate calls for every mediaId I'm getting back. Populate would do that for me I think, but it doesn't work with subscribe.

jperasmus commented 4 years ago

I'm struggling to reproduce this. I've set up a test for a collection using fields, populate and filters and different combinations of all of them, but so far they are working as expected.

What I did find is that nested fields inside a populate object option does not work, but that is different from what you're having here.

If you're running it client-side, as a test, can you try opening up your database and storage rules to allow for unauthorized access or make sure you are logged in first before this query runs?

If this still doesn't work, can you give me an export of your schema as well as an example content entry that I can set up on my side to see if I can reproduce it with your exact data?

For populate, can you also try setting it to populate: true instead of specifying the exact fields you want to expand?

erik-slack commented 4 years ago

This is all being done clientSide. When I'm not logged in I get an error in my console. The problem I'm experiencing is I just get back an empty results collection.

Here's a new example:

this.demosRef = this.flamelinkService.getApp().content
      .subscribe({
        schemaKey: 'demos',
        fields: ['name', 'videoUrl', 'description'],
        populate: true,
        callback: (error, data) => {
          if (error) {
            return console.error(error);
          }

          console.log('demos', data);
        }
      });

All it does is return the following. image

When I remove the fields option and just do populate: true then it does in fact return values, but it's the same as if I hadn't put populate: true--it just does nothing. I expected that populate true would turn the [#####] into a string url to the image.

image

Also I'd very much like to expand certain fields at some point. I hope you can get that working too.

Please note I'm using RTDB not CF in case that matters.

erik-slack commented 4 years ago

I'm not sure how to give you an export of the schema. So here's a screenshot of how everything is set up and another for how the image which I am trying to populate is configured. The last is of a piece of content for that schema.

image

image

image

jperasmus commented 4 years ago

Thanks Erik, I'll take a deeper look. I was testing with Cloud Firestore, so I think the problem is probably specific to the RTDB.

jperasmus commented 4 years ago

Hi Erik, just letting you know I haven't forgotten about this. I have just been travelling and busy with other pressing issues, but I'll get back to you soon.

If this is urgent, you can take a look at the content-rtdb package in this repo if you can see what is causing the problem.

I looked quickly now and it looks like the subscribe method is not running the populate functionality as compared to the get method. In your case, does it work if you use app.content.get() instead of app.content.subscribe()?

jperasmus commented 4 years ago

Hi @erik-slack, I am on leave from today, but I wanted to get this one resolved for you before I can actually enjoy the leave. :) Can you please upgrade to v1.0.0-alpha.27 and see if it works for you now?

Side note: the reason I thought at first you were using Cloud Firestore was because of the filters option. That is only for CF. The RTDB doesn't have as powerful querying capabilities, so the options are limited. See the docs here: https://flamelink.github.io/flamelink-js-sdk/#/api-overview?id=realtime-database-2

erik-slack commented 4 years ago

@jperasmus thanks for the update! I've got populate working now which saves me a ton of code and stops my app from visibly painting with promises. Thanks for letting me know about the filters only working with CF. Sorry about my late response!

erik-slack commented 4 years ago

Closing this as my concerns have been resolved with v1.0.0-alpha.27

jperasmus commented 4 years ago

Thanks, Erik, I'm glad it is working now. 👍

McCaul95 commented 3 years ago

Hi Erik Did you get a working version of this i tried your repo on github but cant seem to get the data to return anything other than undefined