Nuxt3 Supabase createClient Server error "main.createClient is not a function" on build

huangapple go评论132阅读模式
英文:

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!

{
    &quot;url&quot;: &quot;/api/incoming-message&quot;,
    &quot;statusCode&quot;: 500,
    &quot;statusMessage&quot;: &quot;&quot;,
    &quot;message&quot;: &quot;main.createClient is not a function&quot;,
    &quot;stack&quot;: &quot;&quot;
}
import { Database, Json } from &#39;~/types/supabase&#39;;
import twilio from &#39;twilio&#39;;
import { createClient } from &#39;@supabase/supabase-js&#39;;

export default defineEventHandler(async (event) =&gt; {
	const config = useRuntimeConfig();

	const supabase = createClient&lt;Database&gt;(
		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&lt;Database&gt;(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

huangapple
  • 本文由 发表于 2023年2月7日 03:25:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定