简体   繁体   中英

Pinia: Cannot access store from Nuxt3 API

I have a Nuxt3 project with an Api in server/api/<api-endpoints> . Inside an endpoint, when I try to use a Pinia Store it says:

[nuxt] [request error] []: getActivePinia was called with no active Pinia. Did you forget to install pinia?

or when I try to access a composable function from composables it cannot define any variable (says xxx is undefined even though it works in components so it just can't define anything in the function).

Pinia example inside server/api/token

import { useAuthStore } from "../stores/useAuthStore";
export default async (req: any, res: any) => {
    const authStore = useAuthStore()
    return authStore.access_token
};

Composable example inside server/api/user

import { useUserLogin } from "../../composables/useUserLogin";
export default async (req: any, res: any) => {
    const userLogin = useUserLogin()
    return twitchLogin.is_logged_in
};

I solved it by renaming buildModules to modules in nuxt.config.js

Nuxt modules are now build-time-only, and the buildModules property used in Nuxt 2 is deprecated in favor of modules.

// nuxt.config.js
export default defineNuxtConfig({
  modules: [
    // ...
    '@pinia/nuxt',
  ],
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM