Error: Error: User with clerkId user_2kQEy3NVpSvdKbiMsH2yBVKhHof not found
Opened this issue · 1 comments
HERE IS MY CODES " "use server";
import { revalidatePath } from "next/cache";
import User from "../database/models/user.model";
import { connectToDatabase } from "../database/mongose";
import { handleError } from "../utils";
// CREATE
export async function createUser(user: CreateUserParams) {
try {
await connectToDatabase();
const newUser = await User.create(user);
return JSON.parse(JSON.stringify(newUser));
} catch (error) {
handleError(error);
}
}
// READ
export async function getUserById(userId: string) {
try {
await connectToDatabase();
console.log(`Fetching user with clerkId: ${userId}`); // Log userId
const user = await User.findOne({ clerkId: userId });
if (!user) {
console.error(`User not found with clerkId: ${userId}`); // Log error
throw new Error(`User with clerkId ${userId} not found`);
}
return JSON.parse(JSON.stringify(user));
} catch (error) {
handleError(error);
}
}
// UPDATE
export async function updateUser(clerkId: string, user: UpdateUserParams) {
try {
await connectToDatabase();
console.log(`Updating user with clerkId: ${clerkId}`); // Log clerkId
const updatedUser = await User.findOneAndUpdate({ clerkId }, user, {
new: true,
});
if (!updatedUser) {
console.error(`User update failed for clerkId: ${clerkId}`); // Log error
throw new Error(`User update failed for clerkId ${clerkId}`);
}
return JSON.parse(JSON.stringify(updatedUser));
} catch (error) {
handleError(error);
}
}
// DELETE
export async function deleteUser(clerkId: string) {
try {
await connectToDatabase();
console.log(`Deleting user with clerkId: ${clerkId}`); // Log clerkId
const userToDelete = await User.findOne({ clerkId });
if (!userToDelete) {
console.error(`User not found for deletion with clerkId: ${clerkId}`); // Log error
throw new Error(`User not found with clerkId ${clerkId}`);
}
const deletedUser = await User.findByIdAndDelete(userToDelete._id);
revalidatePath("/");
return deletedUser ? JSON.parse(JSON.stringify(deletedUser)) : null;
} catch (error) {
handleError(error);
}
}
// USE CREDITS
export async function updateCredits(userId: string, creditFee: number) {
try {
await connectToDatabase();
console.log(`Updating credits for user with _id: ${userId}`); // Log userId
const updatedUserCredits = await User.findOneAndUpdate(
{ _id: userId },
{ $inc: { creditBalance: creditFee }},
{ new: true }
);
if (!updatedUserCredits) {
console.error(`User credits update failed for _id: ${userId}`); // Log error
throw new Error(`User credits update failed for _id ${userId}`);
}
return JSON.parse(JSON.stringify(updatedUserCredits));
} catch (error) {
handleError(error);
}
}
" @adrianhajdin
i had the same issue i fixed it awaiting the auth in the transformations>add>type>page.tsx, like this
const { userId }: { userId: string | null } = await auth();