在generateStaticParams中从createServerClient导入supabase时出错- Next js 13

9fkzdhlc  于 2023-08-04  发布在  其他
关注(0)|答案(1)|浏览(98)

我正在将我的应用程序从nextjs 12迁移到nextjs 13,在迁移我的[...slug].tsx文件时遇到了一些问题。
我从一个简单的代码片段开始,但我得到了这个错误:

Error: Invariant: Method expects to have requestAsyncStorage, none available

字符串
我的新页面在[user-profile]下,在/app下。

import * as React from "react";
import { createServerClient } from "../../lib/supabase-server";

// Return a list of `params` to populate the [slug] dynamic segment
export async function generateStaticParams() {
  const supabase = createServerClient();
  const { data: profiles } = await supabase.from("user").select("username");

  if (!profiles) {
    throw new Error("No profiles found");
  }

  return profiles.map((profile) => ({
    slug: profile,
  }));
}

export default async function Page({
  params,
}: {
  params: { ["user-profile"]: string };
}) {
  return <div>here</div>;
}


createServerClient()文件如下所示:

import { headers, cookies } from "next/headers";
import { createServerComponentSupabaseClient } from "@supabase/auth-helpers-nextjs";
import { Database } from "../ts/models/database-types";

export const createServerClient = () =>
  createServerComponentSupabaseClient<Database>({
    headers,
    cookies,
    supabaseKey: process.env.NEXT_PUBLIC_SUPABASE_ANON_PUBLIC,
    supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_PROJECT_URL,
  });


我的想法是在mydomain.com/username-example中呈现所有用户,我无法克服这个错误。有人能帮帮我吗?

4bbkushb

4bbkushb1#

在此Thread中找到解决方案

import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";
import { Database } from "@/lib/types/schema";
import { cookies } from 'next/headers'
import { cache } from 'react';

export const createServerClient = cache(() => {
    const cookieStore = cookies()
    return createServerComponentClient<Database>({
        cookies: () => cookieStore
    })
})

export async function GET() {
    const supabase = createServerClient()

    const { data, error } = await supabase.from('').select('')

    return data
}

字符串
让我知道如果这对你有用。

相关问题