使用supabase cdn时错误:createClient未定义

问题描述 投票:0回答:2

我正在为一个项目使用 Supabase CDN。当我调用

createClient()
函数时,出现此错误:

createClient is not defined

我用旧版本的CDN、另一个浏览器并使用

SupabaseClient.createClient
的旧方法进行了尝试,但我得到了:

SupabaseClient is not defined

CDN 工作不正常还是其他原因?

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>HTML</title>

  <!-- Custom Styles -->
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <p>Nothing to see here...</p>

  <!-- Project -->

  <script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2"></script>

  <script src="main.js"></script>
</body>
</html>
const SupabaseKey = // api here
const SupabaseUrl =// URL here

const options = {
  db: {
    schema: 'public',
  },
  auth: {
    autoRefreshToken: true,
    persistSession: true,
    detectSessionInUrl: true
  }
}

const supabase = createClient(SupabaseUrl, SupabaseKey, options)
cdn supabase
2个回答
2
投票

我通过在 JS 文件中添加 import 语句来修复它,如下所示:

import createClient from 'https://cdn.jsdelivr.net/npm/@supabase/supabase-js/+esm'

URL 末尾的 +esm 来自通用模块,现在可以使用。


-1
投票

我做了同样的事情,但它不起作用,在浏览器中出现: 未捕获的语法错误:导入声明可能只出现在模块的顶层

<!DOCTYPE html>
<html>
<head>
    <script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2"></script>
</head>
</html>
<script>
    import createClient from 'https://cdn.jsdelivr.net/npm/@supabase/supabase-js/+esm'
    const supabaseUrl = 'URL'
    const supabaseKey = 'KEY'
    const supabase = createClient(supabaseUrl, supabaseKey)
</script>

© www.soinside.com 2019 - 2024. All rights reserved.