所以我有一个 Next js 应用程序,并且我正在从 supabase 数据库检索帖子。当我检索所有帖子时,它可以工作,但是当我使用 Id 动态检索单个帖子时,屏幕显示 null。 文件夹结构:
在
app/notes/page.tsx
中,这是检索所有帖子的代码,也称为赏金。
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";
import { cookies } from "next/headers";
import { createSupabaseAppServerClient } from "@/utils/supabase/supabaseAppRouterClient";
import {createBrowserClient, createServerClient} from "@supabase/ssr";
export default async function Notes() {
// const supabase1 = createSupabaseAppServerClient(true);
const supabase = createServerComponentClient<any, "public", any>( { cookies},
{supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,});
const { data } = await supabase.from("bounties").select();
return(
<pre>{JSON.stringify(data, null, 2)}</pre>
);
}
在
app/pages/notes/[...id]
中,这是当根据其id调用特定注释时在屏幕上返回null的代码。
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";
import { cookies } from "next/headers";
export default async function Page(
{params : {id}} : {params: {id: string}}
) {
const supabase = createServerComponentClient<any, "public", any>( { cookies},
{supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,});
const { data} = await supabase.from('bounties').select().eq('id', 'bounty_id').single();
return(
<pre>{JSON.stringify(data, null, 2)}</pre>
);
}
您没有将
id
变量传递到 eq
过滤器。尝试以下操作:
const { data} = await supabase.from('bounties').select().eq('id', id).single();