next.js Invariant:方法需要requestAsyncStorage,但没有可用的

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

我试图检索使用prisma客户端从mongodbMap集的用户的数据,我写了这个代码的数据提取,它显示错误,这里的prisma客户端代码写在prismadb文件导入为prisma

import { NextApiRequest, NextApiResponse } from "next";
import prisma from "./prismadb";
import { getServerSession } from "next-auth";

const serverAuth = async (req: NextApiRequest, res: NextApiResponse) => {
    try {
        const session = await getServerSession(req);

        if (!session?.user?.email) {
            throw new Error('Not signed in');
        }

        const currentUser = await prisma.user.findUnique({
            where: {
                email: session.user.email,
            }
        });

        if (!currentUser) {
            throw new Error('Not signed in');
        }

        return { currentUser };
    } catch (error:any) {
        // res.status(500).json({ error: `&{err.message}` });
        res.status(500).json({ error: error.message });
        return;
    }
};

export default serverAuth;

字符串
我已经给出了try和catch,并且出现了这个错误。我在聊天GPT中问过,它表明这可能是由于next.js和next-auth之间的一些错误,并且在问题的官方GitHub帐户被关闭,但我不明白任何事情
以下是参考链接:

在next-auth https://github.com/nextauthjs/next-auth/issues/6989

ulmd4ohb

ulmd4ohb1#

在这个线程中找到了解决方案

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
}

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

相关问题