Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invariant: Method expects to have requestAsyncStorage, none available

I am trying to retrive the data of a user from mongodb atlas using prisma client and I write this code for the fetching of the data and It shows error, Here the prisma client code are written in the prismadb file which is imported as 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;

I have given the try and catch and this error shows up. I have asked in the chat GPT and it suggests that this may be due to some error between next.js and next-auth and in teh official GitHub account of the Issue is closed but I don't understand any thing

Here are the reference links:

  • https://github.com/vercel/next.js/issues/46356
  • https://github.com/vercel/next.js/issues/45371

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

like image 713
Ashutosh Hota Avatar asked Nov 17 '25 03:11

Ashutosh Hota


2 Answers

If you're getting this error whiles using next13, make sure the file in which you have getServerSession is actually a server file. include "use server" at the top of your page.

getServerSession has to be called in a server component.

like image 191
ninsau Avatar answered Nov 18 '25 19:11

ninsau


Found a solution in this 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
}

Let me know if this works for you.

like image 28
dbustosp Avatar answered Nov 18 '25 20:11

dbustosp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!