英文:
Nuxt3 Supabase createClient Server error "main.createClient is not a function" on build
问题
我正在尝试在 Nuxt 3 的服务器/api/myroute.ts 中实例化 Supabase。在我的本地开发网络上可以正常工作,但当我部署到 Netlify 并使用 Postman 请求路由时,出现以下错误。问题点在于尝试使用 createClient 创建 Supabase 实例后的那一部分。在 Netlify 上构建测试时出现了问题,本地网络上运行此代码没有任何问题。
如果有人能帮助解决这个问题,我将不胜感激,谢谢!
{
"url": "/api/incoming-message",
"statusCode": 500,
"statusMessage": "",
"message": "main.createClient 不是一个函数",
"stack": ""
}
import { TwilioIncoming } from '~/types/types';
import { Database, Json } from '~/types/supabase';
import twilio from 'twilio';
import { createClient } from '@supabase/supabase-js';
export default defineEventHandler(async (event) => {
const config = useRuntimeConfig();
const supabase = createClient<Database>(
config.public.SUPABASE_URL,
config.private.SUPABASE_SERVICE_KEY
);
希望这对你有所帮助。如果需要进一步的指导,请随时提出。
英文:
I am trying to instantiate supabase in nuxt3 in the server/api/myroute.ts . It works on my local dev network but when I deploy it to netlify and use postman to hit the route I get the error below. The breaking point is right after it tries to create the supabase instance with create client. The endpoint is being tested on the build on netlify the local network does not have any issues running this code.
If anyone can help with this I would appreciate it, thank you!
{
"url": "/api/incoming-message",
"statusCode": 500,
"statusMessage": "",
"message": "main.createClient is not a function",
"stack": ""
}
import { Database, Json } from '~/types/supabase';
import twilio from 'twilio';
import { createClient } from '@supabase/supabase-js';
export default defineEventHandler(async (event) => {
const config = useRuntimeConfig();
const supabase = createClient<Database>(
config.public.SUPABASE_URL,
config.private.SUPABASE_SERVICE_KEY
);
答案1
得分: 1
我找到了一个解决方法,当使用服务角色密钥初始化Supabase客户端,并且之前没有使用匿名令牌初始化Supabase客户端时,就会发生错误。
serverSupabaseClient(event); // 使用匿名令牌创建Supabase客户端
const supabase = serverSupabaseServiceRole<Database>(event);
如果有人找到更好的解决方案,我愿意倾听。
编辑:我的示例使用 @nuxtjs/supabase
,但你也可以使用 supabase-js
来实现相同的效果。
英文:
I found a workaround, the error occures when initializing a supabase client using the service role key and no supabase client has been initialized before using the anon token.
serverSupabaseClient(event); // Create a supabase client using anon token
const supabase = serverSupabaseServiceRole<Database>(event);
If someone find a better solution, I'm all ears.
EDIT: My example uses @nuxtjs/supabase
but you can manage to do the same using supabase-js
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论