Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Error: User with clerkId user_2kQEy3NVpSvdKbiMsH2yBVKhHof not found #32

Open
Legit-Sam opened this issue Aug 10, 2024 · 1 comment

Comments

@Legit-Sam
Copy link

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
Screenshot 2024-08-10 123827

@PhatDoge
Copy link

PhatDoge commented Dec 15, 2024

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants