takumikunn15
takumikunn15

Reputation: 31

Nuxt.js3 plugin's context becomes undefined

I am developing an application using Nuxt.js3 and supabase.

Nuxt.js in plugins/supabase.server.js (I haven't figured out if server or client is better for this too.) I want to use "supabase = createClient(~~)" from index.vue.

However, I get undefined, either because the import is not working or because I am calling it the wrong way.

If I use the mustache syntax and call it like "{{ $supabase }}", the function will appear.

(I am not good at English, so I use translated sentences.)

plugins/supabase.server.js

import { defineNuxtPlugin } from '#app'
import { createClient } from '@supabase/supabase-js/dist/main/index.js'

export default defineNuxtPlugin(nuxtApp => {
  const config = useRuntimeConfig();
  nuxtApp.provide('supabase', () => createClient(config.supabaseUrl, config.supabaseKey))
})
declare module '#app' {
  interface NuxtApp {
    $supabase (): string
  }
}

pages/index.vue

<script setup>
console.log($supabase) //$supabase is not defined
</script>
<template>
{{ $supabase }} // () => createClient(config.supabaseUrl, config.supabaseKey)

</template>

Upvotes: 3

Views: 4259

Answers (1)

user2929594
user2929594

Reputation: 263

Please import useRuntimeConfig from '#app'. So in your example change the first line:

import { defineNuxtPlugin } from '#app'

Into:

import { defineNuxtPlugin, useRuntimeConfig } from '#app'

Upvotes: 3

Related Questions