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

Cannot read properties of undefined (reading 'filter') #3295

Closed
mrleblanc101 opened this issue Jan 6, 2025 · 2 comments · Fixed by #3324
Closed

Cannot read properties of undefined (reading 'filter') #3295

mrleblanc101 opened this issue Jan 6, 2025 · 2 comments · Fixed by #3324
Assignees
Labels

Comments

@mrleblanc101
Copy link
Contributor

mrleblanc101 commented Jan 6, 2025

Environment



Reproduction

https://stackblitz.com/edit/github-9ytrzgtu?file=pages%2Findex.vue

Describe the bug

As soon as I add an i18n custom block to my component, the app crash with this error:

Cannot read properties of undefined (reading 'filter')

The code for the language switcher comes from the documentation (except the console.log):

<script lang="ts" setup>
const { locale, locales, t } = useI18n();
const switchLocalePath = useSwitchLocalePath();

const availableLocales = computed(() => {
    console.log(locales, locales.value);
    return locales.value.filter((i) => i.code !== locale.value);
});
</script>

I notice that locales is sometime a ComputedRef and sometime a Reactive.

Where there is a <i18n> custom block:
Image

When there isn't one:
Image

That would explain why the locales.value.filter since a Reactive doesn't need a .value

Additional context

No response

Logs

@mrleblanc101
Copy link
Contributor Author

mrleblanc101 commented Jan 6, 2025

I think I understand my error, but it's not explained at all in the Nuxt-i18n documentation why there is sometime a useScope: 'local' in the useI18n() call.

I changed my code for this:

const { locale, locales } = useI18n({ useScope: 'global' });
const { t } = useI18n({ useScope: 'local' });

Now I don't have the error:

Cannot read properties of undefined (reading 'filter')

Feel free to close if there is no change needed to the documentation.

@BobbieGoede
Copy link
Collaborator

Thank you for reporting, I think I know what the source of this behavior, I ran into it (or something similar) during recent refactors but preserved the original behavior as I wasn't sure if it was intended either.

I'm going to check if I can keep the behavior the same regardless of the presence of <i18n> block or local scope usage 🙏.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants