我如何从supabase检索单个帖子并使用Nextjs14在动态路由中显示

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

所以我有一个 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>
    );
}
javascript next.js routes dynamic supabase
1个回答
0
投票

您没有将

id
变量传递到
eq
过滤器。尝试以下操作:

    const { data} = await supabase.from('bounties').select().eq('id', id).single();

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