Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lazy-mode] this.$fireModule returns null IF this.$fire...Ready() is called in the same component #366

Closed
ismail-fathi opened this issue Nov 5, 2020 · 21 comments
Assignees
Labels
bug Something isn't working docs Fixes or questions regarding the documentation.

Comments

@ismail-fathi
Copy link

Version

@nuxtjs/firebase:
firebase:
nuxt:

Steps to reproduce

setup firebase as you normally do in nuxtjs
then call await this.$fire.firestoreReady()
then console.log(this.$fireModule)

What is actually happening?

the output is undifined

@lupas
Copy link
Member

lupas commented Nov 5, 2020

Hey @ismail-fathi

Hard to debug without you sharing your code. Did you install Firebase? Could you share your package.json?

@lupas lupas added the question A general question about the module or Firebase. label Nov 5, 2020
@ismail-fathi
Copy link
Author

ismail-fathi commented Nov 5, 2020

Hey @lupas

I have installed firebase and @nuxtjs/firebase

"firebase": "^8.0.0"
"@nuxtjs/firebase": "^7.0.0"

And that is my nuxtjs/firebase config

'@nuxtjs/firebase',
      {
        config: {
          apiKey: "XXXX",
          authDomain: "XXXX",
          databaseURL: "XXXX",
          projectId: "XXXX",
          storageBucket: "XXXX",
          messagingSenderId: "XXXX",
          appId: "XXXX",
          measurementId: "XXXX"
        },
        lazy: true,
        services: {
          auth: true,
          firestore: true,
          functions: true,
          storage: true
        },

In my page I have a method where I want to use firestore but when I try to usethis.$fireModule) it returns undefined

submit() {
        await this.$fire.firestoreReady()
        console(this.$fireModule)
      }

I hope you now get a better understanding of what is wrong and thank you for your help!

@lupas
Copy link
Member

lupas commented Nov 5, 2020

@ismail-fathi

$fireModule gets injected after the first ready() function is finished. Can you try making your submit function async so that it awaits firestoreReady() before it continues?

Like so:

async submit() {
        await this.$fire.firestoreReady()
        console(this.$fireModule)
      }

@ismail-fathi
Copy link
Author

@lupas

my function is indeed async but still..

image

but still returns undefined

@lupas
Copy link
Member

lupas commented Nov 5, 2020

Hmmmm, interesting. I just tested it and the same works for me perfectly:

Did you add the module to modules resp. buildModules?

modules: ['@nuxtjs/firebase'],

@ismail-fathi
Copy link
Author

yep it's in modules indeed

shoud ['@nuxtjs/firebase'] be in buildModules?

@lupas
Copy link
Member

lupas commented Nov 5, 2020

Can be in buildModules in this case, but this should not make a difference...

Hm... you can log out console.log(this.$fire) and it works?

@ismail-fathi
Copy link
Author

Yep it works
here is the result

image

@lupas
Copy link
Member

lupas commented Nov 5, 2020

My bad, I was already loading another Firebase service in a plugin (context.$fire.authRead()), that's why it worked for me. If the very first ready() function is called directly in a vue component, it indeed seems not to be accessible. (though the inject() function get's triggered...)

@pimlie
Do you maybe know how injections works under the hood? If I call somethingReady() in a plugin, I can later use this.$fireModule in the context because it gets injected "early enough". But if I use it directly in a vue component, the inject function gets triggered - but $fireModule is still undefined.

Any idea?

@lupas lupas added bug Something isn't working and removed question A general question about the module or Firebase. labels Nov 5, 2020
@lupas lupas pinned this issue Nov 12, 2020
@lupas lupas changed the title this.$fireModule returns null [lazy-mode] this.$fireModule returns null IF this.$fire...Ready() is called in the same component Nov 12, 2020
@abhishekbatra
Copy link

Just upgraded and facing this issue. Should lazy just be disabled for now as a workaround?

@pimlie
Copy link
Member

pimlie commented Dec 9, 2020

@lupas Sorry for late reply, injecting like this doesnt work reliably indeed. Sorry about that, we/Rafal found this out when I added lazy initialization for the nuxt sentry/module where I used the same injection scheme.

For the sentry module we added this custom inject helper: https://github.com/nuxt-community/sentry-module/blob/master/lib/plugin.lazy.js#L186 which is called here: https://github.com/nuxt-community/sentry-module/blob/master/lib/plugin.lazy.js#L164. The problem is is that Nuxt uses a defineProperty to add the injection without a setter, so once you've injected something you cannot overwrite it anymore. See here for Nuxt's inject implementation: https://github.com/nuxt/nuxt.js/blob/dev/packages/vue-app/template/index.js#L181-L216

@1hakr
Copy link

1hakr commented Dec 16, 2020

Apart from that issue, I also noticed that with lazy: true, onAuthStateChangedAction is not getting triggered.

@lupas
Copy link
Member

lupas commented Dec 16, 2020

Thanks @pimlie, will have a look at if we could solve this similarly in this module.
Until this is fixed I guess I will just mention in the docs that we have this limitation in lazy mode.

@1hakr About onAuthStateChangedAction not working in lazy mode: This is clear - if we do not initialize auth from the beginning, we of course also cannot call onAuthStateChanged, since auth is not initialized. So if someone wants to use onAuthStateChanged, lazy mode does not make sense. OR one can already load the auth module in a plugin before onAuthStateChange is called, that would of course work.

I thought we documented this somewhere but it seems we missed it... thanks for the input!

ToDo notes for myself or anyone who has time:

  • Add missing info for onAuthStateChangedAction to documentation
  • Try to fix this issue

@lupas lupas self-assigned this Dec 16, 2020
@lupas lupas added the docs Fixes or questions regarding the documentation. label Dec 16, 2020
lupas added a commit that referenced this issue Jan 17, 2021
@lupas
Copy link
Member

lupas commented Jan 17, 2021

Hey @ismail-fathi & Co.

Finally got to fixing this issue with the solution from @pimlie (which was plain simple) - so thanks you too, I appreciate it.

Should now be working in v7.3.1

@lupas lupas closed this as completed Jan 17, 2021
@lupas lupas unpinned this issue Jan 17, 2021
@uppergoal
Copy link

Hey! I'm facing the problem where I'm enabling lazy and I'm trying to await app.$fire.authReady() inside a plugin. I'm receiving the following error:

TypeError: Cannot read property '$options' of undefined
    at forceInject (index.js?d90b:170)

lupas added a commit that referenced this issue Jan 17, 2021
… in a plugin in lazy mode

fixed forceInject failing when ...ready() is called in a plugin in lazy mode

re #366
@lupas
Copy link
Member

lupas commented Jan 17, 2021

Thanks a lot @uppergoal , fixed that with v7.3.2

@uppergoal
Copy link

Thanks a lot @uppergoal , fixed that with v7.3.2

The problem is still happening for me with v7.3.2 (Since version v7.3.0). Version 7.2.3 worked fine so I'll downgrade to that version.

@lupas
Copy link
Member

lupas commented Jan 17, 2021

Thanks a lot @uppergoal , fixed that with v7.3.2

The problem is still happening for me with v7.3.2 (Since version v7.3.0). Version 7.2.3 worked fine so I'll downgrade to that version.

Hmm... will have a look at it tomorrow, I'm quite sure I had it working with the fix. Same error message?

@uppergoal
Copy link

Thanks a lot @uppergoal , fixed that with v7.3.2

The problem is still happening for me with v7.3.2 (Since version v7.3.0). Version 7.2.3 worked fine so I'll downgrade to that version.

Hmm... will have a look at it tomorrow, I'm quite sure I had it working with the fix. Same error message?

Yeah sadly. I wish I could propose a fix to help you.

This is the error I have:
image

In my plugin: await app.$fire.authReady() is creating the error.

**my nuxt.config: **
ssr: false,
target: 'static'

firebase: {
lazy: true
},
auth: {
        initialize: {
          onAuthStateChangedAction: 'auth/onAuthStateChanged',
          subscribeManually: true,
        },
        ...
        }

Version 7.2.3. is working perfectly fine.

lupas added a commit that referenced this issue Jan 18, 2021
@lupas
Copy link
Member

lupas commented Jan 18, 2021

@uppergoal
Alright, NOW it should be fixed in v7.3.3. :) Thanks for the tip, appreciate it.

@uppergoal
Copy link

@uppergoal
Alright, NOW it should be fixed in v7.3.3. :) Thanks for the tip, appreciate it.

Awesome! I just tested it and it works. Thanks @lupas for your work. Greatly appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working docs Fixes or questions regarding the documentation.
Projects
None yet
Development

No branches or pull requests

6 participants