nuxt-modules / i18n

I18n module for Nuxt
https://i18n.nuxtjs.org
MIT License
1.75k stars 483 forks source link

Cannot read properties of undefined (reading 'messages') #3010

Closed alexcroox closed 3 months ago

alexcroox commented 4 months ago

Environment

Reproduction

Passing an H3 event from an API eventHandler to a function in the server/utils directory causes this error:

Cannot read properties of undefined (reading 'messages')

Describe the bug

My function where the error originates:

image

Looking at the stack trace at the bottom I think I've manage to figure out why it's erroring with this.

Firstly the localeDector is called here with 1 param (event)

image

This seems to be calling this localeDetector function which requires the i18nContext context as it's second param.

image

Which is why this line fails:

await loadAndSetLocaleMessages(locale, localeLoaders, i18nContext.messages);

because i18nContext is undefined.

 ERROR  Cannot read properties of undefined (reading 'messages')

  at localeDetector (.nuxt/dev/index.mjs:3233:73)
  at useTranslation (node_modules/@intlify/h3/dist/index.mjs:38:39)
  at generateAndSaveTokens (.nuxt/dev/index.mjs:2717:19)
  at Object.handler (.nuxt/dev/index.mjs:6889:47)
  at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  at async node_modules/h3/dist/index.mjs:1962:19
  at async Object.callAsync (node_modules/unctx/dist/index.mjs:72:16)
  at async Object.callAsync (node_modules/unctx/dist/index.mjs:72:16)
  at async Server.toNodeHandle (node_modules/h3/dist/index.mjs:2249:7)

Additional context

No response

Logs

No response

github-actions[bot] commented 4 months ago

Would you be able to provide a reproduction? πŸ™

More info ### Why do I need to provide a reproduction? Reproductions make it possible for us to triage and fix issues quickly with a relatively small team. It helps us discover the source of the problem, and also can reveal assumptions you or we might be making. ### What will happen? If you've provided a reproduction, we'll remove the label and try to reproduce the issue. If we can, we'll mark it as a bug and prioritise it based on its severity and how many people we think it might affect. If `needs reproduction` labeled issues don't receive any substantial activity (e.g., new comments featuring a reproduction link), we'll close them. That's not because we don't care! At any point, feel free to comment with a reproduction and we'll reopen it. ### How can I create a reproduction? We have a couple of templates for starting with a minimal reproduction: πŸ‘‰ [Reproduction starter (v8 and higher)](https://stackblitz.com/fork/github/BobbieGoede/nuxt-i18n-starter/tree/v8) πŸ‘‰ [Reproduction starter (edge)](https://stackblitz.com/fork/github/BobbieGoede/nuxt-i18n-starter/tree/edge) A public GitHub repository is also perfect. πŸ‘Œ Please ensure that the reproduction is as **minimal** as possible. See more details [in our guide](https://nuxt.com/docs/community/reporting-bugs/#create-a-minimal-reproduction). You might also find these other articles interesting and/or helpful: - [The Importance of Reproductions](https://antfu.me/posts/why-reproductions-are-required) - [How to Generate a Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve)
github-actions[bot] commented 4 months ago

Would you be able to provide a reproduction? πŸ™

More info ### Why do I need to provide a reproduction? Reproductions make it possible for us to triage and fix issues quickly with a relatively small team. It helps us discover the source of the problem, and also can reveal assumptions you or we might be making. ### What will happen? If you've provided a reproduction, we'll remove the label and try to reproduce the issue. If we can, we'll mark it as a bug and prioritise it based on its severity and how many people we think it might affect. If `needs reproduction` labeled issues don't receive any substantial activity (e.g., new comments featuring a reproduction link), we'll close them. That's not because we don't care! At any point, feel free to comment with a reproduction and we'll reopen it. ### How can I create a reproduction? We have a couple of templates for starting with a minimal reproduction: πŸ‘‰ [Reproduction starter (v8 and higher)](https://stackblitz.com/fork/github/BobbieGoede/nuxt-i18n-starter/tree/v8) πŸ‘‰ [Reproduction starter (edge)](https://stackblitz.com/fork/github/BobbieGoede/nuxt-i18n-starter/tree/edge) A public GitHub repository is also perfect. πŸ‘Œ Please ensure that the reproduction is as **minimal** as possible. See more details [in our guide](https://nuxt.com/docs/community/reporting-bugs/#create-a-minimal-reproduction). You might also find these other articles interesting and/or helpful: - [The Importance of Reproductions](https://antfu.me/posts/why-reproductions-are-required) - [How to Generate a Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve)
alexcroox commented 4 months ago

it's this line here, should I be adding the issue on the intlify/h3 repo instead?

https://github.com/intlify/h3/blob/main/src/index.ts#L355

That line is calling the line in this repo that expects the second param:

https://github.com/nuxt-modules/i18n/blob/3c3277ef172209ae1d4ce23d4b5427a476d95c76/src/runtime/server/plugin.ts#L36-L39

BobbieGoede commented 4 months ago

You could open an issue on the other repository but we will need a (minimal) reproduction of your issue to determine whether this is caused by configuration or a bug.

BobbieGoede commented 3 months ago

Closing due to inactivity and lack of minimal reproduction.

temitopeadesoji commented 1 week ago

Hi @alexcroox, were you able to resolve this issue, i am experiencing similar behaviour intermittently.

alexcroox commented 1 week ago

Unfortunately not. It happens consistently on my login endpoint the very first time I submit after a page reload. After that first usage it doesn’t happen again. Very confused

BobbieGoede commented 1 week ago

If you could create a reproduction I can reopen this and take a look at it πŸ™

alexcroox commented 1 week ago

@temitopeadesoji I found a workaround, set lazy: false in nuxt.config and the error will not occur:

export default defineNuxtConfig({
  i18n: {
    lazy: false,
  }
})

This is because the loadAndSetLocaleMessgaes() function that tries to use an undefined i18nContext is never called if you don't use lazy.

Image

Not ideal from a performance point of view, but it will get rid of the fatal error for now.

@BobbieGoede I'll try get a reproduction together, but the issue is around i18nContext being undefined at times.

alexcroox commented 1 week ago

I tried to reproduce but failed to, still trying to work out what's the flow my full app uses that's causing issues.

https://stackblitz.com/edit/github-z5l3te?file=nuxt.config.ts,layers%2Fauth%2Fserver%2Fapi%2Ftest.get.ts,server%2Futils%2Ftest-util.ts,app%2Fcomponents%2FTest.vue