Already defined role field as 'user' in Schema but not working
Unanswered
Northeast Congo Lion posted this in #help-forum
Northeast Congo LionOP
I am using MongoDB and clerk to save new user, and for new user default role is 'user' which I already created in schema but new field role is not created in model.
clerk webhook logic inside route file
import mongoose, { model, models, Schema } from "mongoose";
const UserSchema = new Schema({
clerkId: {
type: String,
required: true,
unique: true,
},
email: {
type: String,
required: true,
unique: true,
},
username: {
type: String,
required: true,
unique: true,
},
photo: {
type: String,
required: true,
},
firstName: {
type: String,
},
lastName: {
type: String,
},
role:{
type: String,
enum: ['user', 'admin'],
default: 'user',
}
});
const User = models?.User || model("User", UserSchema)
export default User;clerk webhook logic inside route file
if (eventType === "user.created") {
const { id, email_addresses, image_url, first_name, last_name, username } =
evt.data;
console.log(evt.data, 'evt data')
const user = {
clerkId: id,
email: email_addresses[0].email_address,
username: username!,
firstName: first_name,
lastName: last_name,
photo: image_url,
};
const newUser = await createUser(user);
if (newUser) {
await clerkClient.users.updateUserMetadata(id, {
publicMetadata: {
userId: newUser._id,
},
});
}
return NextResponse.json({ message: "New User Created", user: newUser });
}6 Replies
Northeast Congo LionOP
I tried this approach
in route file (clerk webhook):
add role as user in user object
in server action file:
add role as 'user'
in route file (clerk webhook):
add role as user in user object
f (eventType === "user.created") {
const { id, email_addresses, image_url, first_name, last_name, username } =
evt.data;
console.log(evt.data, 'evt data')
const user = {
clerkId: id,
email: email_addresses[0].email_address,
username: username!,
firstName: first_name,
lastName: last_name,
photo: image_url,
role: 'user'
};
const newUser = await createUser(user);
if (newUser) {
await clerkClient.users.updateUserMetadata(id, {
publicMetadata: {
userId: newUser._id,
},
});
}
return NextResponse.json({ message: "New User Created", user: newUser });
}in server action file:
add role as 'user'
export async function createUser(user: CreateUserParams) {
try {
await connectDB();
const newUser = await User.create({...user, role: 'user'});
console.log("newUser:", newUser);
return JSON.parse(JSON.stringify(newUser));
} catch (error) {
console.log("error:", error);
handleError(error);
}
}Komondor
Did you restart your dev server after changing the schema?
Northeast Congo LionOP
yes
Komondor
Try removing the enum stuff from the schema, just have the role field be a string, like last name. I assume last name is getting set. If you can set role the same way you're setting last name then you know it's an enum problem
@Komondor Try removing the enum stuff from the schema, just have the role field be a string, like last name. I assume last name is getting set. If you can set role the same way you're setting last name then you know it's an enum problem
Northeast Congo LionOP
I got the issue, issue was I used Clerk webhook, and pass live endpoint and I try to set role through localhost, and latest code is in local and old code is on live endpoint for backend.
and I set enum in local, that's why its not passing the role.
actually if I don't use clerk webhook then it should work but becasue clerk endpoint it was not work
and I set enum in local, that's why its not passing the role.
actually if I don't use clerk webhook then it should work but becasue clerk endpoint it was not work
Now its working