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:
and in next-auth https://github.com/nextauthjs/next-auth/issues/6989
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With