0Pricing
Supabase Backend as a Service · 课时

将您的应用连接到 Supabase 客户端

安装 Supabase JavaScript 客户端,使用项目 URL 和匿名密钥进行初始化,并发送第一个请求以确认连接成功。

将您的应用连接到 Supabase 客户端 是 CoddyKit 上的免费 Supabase Backend as a Service 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Supabase Backend as a Service 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Supabase Backend as a Service 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

From Dashboard to Code

Dashboard done — now you talk to Supabase from code using its client library. For JavaScript and TypeScript, reach for supabase-js.

Installing the Client

Add the official supabase-js package to your project with your package manager.

npm install @supabase/supabase-js

Where to Find Your Keys

Under Project Settings > API you will find your Project URL, the browser-safe anon key, and the service_role key for servers only.

Creating the Client

Call createClient once with your URL and anon key, then reuse that single instance everywhere in your app.

import { createClient } from '@supabase/supabase-js';
const supabase = createClient(
  'https://your-project.supabase.co',
  'your-anon-key'
);

Keep Secrets in Env Vars

Never hardcode keys in committed code — read them from environment variables instead.

const url = process.env.SUPABASE_URL;
const key = process.env.SUPABASE_ANON_KEY;
console.log('Configured:', Boolean(url && key));

Your First Query

Run your first query with select. The client returns a data array and an error object, never throwing on its own.

const { data, error } = await supabase
  .from('todos')
  .select('*');
if (error) console.error(error.message);
else console.log(data);

Always Handle the Error

Supabase will not throw on a failed query, so check error every time before you touch data.

function handle(result) {
  if (result.error) return 'Failed: ' + result.error;
  return 'Got ' + result.data.length + ' rows';
}
console.log(handle({ data: [1, 2], error: null }));

Inserting Data

Write a row with insert, passing an array of objects. Add select to get the inserted rows back.

const { data, error } = await supabase
  .from('todos')
  .insert([{ title: 'Learn Supabase', done: false }])
  .select();

Client vs Server Keys

The anon key is guarded by Row-Level Security and safe in the browser. The service_role key bypasses RLS, so keep it server-only.

Confirming the Connection

Quick health check: select a single row. Real data or a clear permission error (not a network error) means your client is wired correctly.

Reusing One Instance

Export your configured client from one module and import it everywhere. A single instance avoids duplicate connections and config drift.

Quick Check

Test your understanding of connecting with the client.

Recap

Recap: you installed supabase-js, built a client from env vars, ran your first select and insert, and learned why anon keys are browser-safe.

常见问题解答

「将您的应用连接到 Supabase 客户端」课时是免费的吗?

是的 — 「将您的应用连接到 Supabase 客户端」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Supabase Backend as a Service 课程的其余内容,请升级到 CoddyKit PRO。 Supabase Backend as a Service 课程共包含 4 节课。

「将您的应用连接到 Supabase 客户端」这节课中我会学到什么?

安装 Supabase JavaScript 客户端,使用项目 URL 和匿名密钥进行初始化,并发送第一个请求以确认连接成功。 你通过在浏览器中直接运行的动手代码来练习 Supabase Backend as a Service,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Supabase Backend as a Service 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Supabase Backend as a Service 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「将您的应用连接到 Supabase 客户端」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Supabase Backend as a Service 课中编写并运行代码吗?

能。每节 Supabase Backend as a Service 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. BaaS 与 Supabase 入门
  2. 设置您的第一个项目
  3. Supabase 仪表板导览
  4. 将您的应用连接到 Supabase 客户端
← 返回 Supabase Backend as a Service