#Nuxt App Context unavailable, can't understand why

67 messages · Page 1 of 1 (latest)

rich arrow
#

A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function.
I get this error in a composable I only use in a setup function, so I'm not really sure of why it appears.
Since it's used in a setup function, the nuxt app context should be available.

Even using runWithContext does not manage to fix the problem

Here is the code:

export default async function useUserList () {
  const messages = useMessages()

  const page = useState('page', () => 1)
  const perPage = useState('perPage', () => 15)

  const userCount = await useAuthenticatedFetch<number>('/api/users/count')
  //                   ERROR HERE
  //                  VVVVVVVVVVVV
  const users = await useAsyncData('users', async () => {
    const from = (page.value - 1) * perPage.value
    const result = await useAuthenticatedFetch<User[]>(`/api/users?from=${from}&size=${perPage.value}`)

    if (result.error.value) {
      messages.showMessage({
        message: result.error.value?.data.message ?? 'Une erreur est survenue',
        type: NotificationType.ERROR
      })
      return
    }

    return result.data.value ?? []
  })

  return {
    userCount,
    users,
    pending: computed(() => userCount.pending.value || users.pending.value),
    page,
    perPage
  }
}
sweet hearth
rich arrow
#

also, I did not get any issues using useFetch, so that's weird

sweet hearth
#

Hm, useState is usable within composables. So that’s fine.

#

Can you try to delete your lockfile, node_modules and .nuxt folder and do a fresh package install?

#

Which version of nuxt are you running?

spiral idol
rich arrow
#

But I found out using experimental.asyncContext in my nuxt config solved some issues, but it seems hydration does not work anymore

spiral idol
#

question, is the error in 1 or 2?

spiral idol
spiral idol
rich arrow
rich arrow
#

without the asyncContext, the error I get is the following:

at Module.useNuxtApp (D:\Code\Js\gestion-location\node_modules\nuxt\dist\app\nuxt.js:182:13)
at Module.useAsyncData (D:\Code\Js\gestion-location\node_modules\nuxt\dist\app\composables\asyncData.js:23:38)
at Module.useUserList [as default] (D:\Code\Js\gestion-location\app\composables\use-userlist.ts:17:45)
at runNextTicks (node:internal/process/task_queues:60:5)
at process.processImmediate (node:internal/timers:447:9)
at async setup (D:\Code\Js\gestion-location\app\components\management\UserList.vue:52:122)
#

so the real issue is the useAsyncData by itself

#

btw, line 17 is an empty line in my use-userlist.ts 😅

spiral idol
rich arrow
#

I don't think so...
The error appeared when I migrated to nuxt 3.8, and before that, I was able to use useRequestHeaders on the client just fine :/

spiral idol
#

No, no, I'm wrong, one second.

#

try this

export function useAuthenticatedFetch () {
  const headers = useRequestHeaders(['cookie']);

  return $fetch.create({
    headers
  });
}
rich arrow
#

and where do I put my request url?

#

oh ok I see

spiral idol
rich arrow
#

does not change anything

#

The error does not come from this method call but from the useAsyncData call

#

I don't see any evidence in the stacktrace of it coming from this part of the code

spiral idol
rich arrow
#

I'll try

spiral idol
#

Also, try doing this

const { data: users, error } = await useAsyncData('users', async () => {
...

console.log('ERROR IN ASYNC DATA', error.value)
rich arrow
#

why?

#

in any case, here is your example

spiral idol
# rich arrow ??

What's wrong with that?
with useAsyncData you need to destructure the data

const { data, pending, error, execute, ...} = await useAsyncData(...
rich arrow
#

I don't see the point of printing something

#

Oh ok I see

#

But I already have something showing me a message if an error happens

#

and it's not the case

#

My route is fine

#

The error is not related to that

spiral idol
rich arrow
#

but since useAsyncData causes the error, its content does not affect anything

#

The error is not caused by what happens inside it, it is caused by it itself

rich arrow
#

so I need to reintroduce them to a synced context in order to make em work

#

awkward but I'll test out

#

Oh, thank you! It worked like a charm

#

But the page does not seem to hydrate, don't know where it comes from but annoying

spiral idol
rich arrow
#

Yep, that's what I'm trying to do

#

still annoying that debugging on nuxt is still so complicated

spiral idol
rich arrow
#

While I'm trying to fix that, do you have an idea of what could be causing hmr to take ~10 seconds to hot reload my page

rich arrow
rich arrow
spiral idol
rich arrow
#

Nope, running barebone node

#

Should I use wsl?

spiral idol
rich arrow
#

Let's delete the node_modules and use yarn on wsl then stonks

spiral idol