ClerkID duplicate error on MongoDB #4839
Unanswered
ADX119
asked this question in
Support request - DO NOT OPEN HERE
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Issue: Duplicate Key Error While Registering Users
Description
I'm encountering a duplicate key error while trying to register multiple users in my application using Express and MongoDB. The error is triggered when attempting to insert a second user.
Error Message
plaintext
MongoServerError: E11000 duplicate key error collection: Narrativ.users index: clerkId_1 dup key: { clerkId: null } errorResponse: {
index: 0,
code: 11000,
errmsg: 'E11000 duplicate key error collection: Narrativ.users index: clerkId_1 dup key: { clerkId: null }',
keyPattern: { clerkId: 1 },
keyValue: { clerkId: null }
},
index: 0,
code: 11000,
keyPattern: { clerkId: 1 },
keyValue: { clerkId: null },
[Symbol(errorLabels)]: Set(0) {}
}
Observations
Schema
Here’s the schema for the User collection:
javascript
import { Schema } from "mongoose";
import mongoose from "mongoose";
const userSchema = new Schema(
{
_id: {
type: Schema.Types.ObjectId,
auto: true, // MongoDB auto-generates this
},
clerkUserId: {
type: String,
// required: true, // Uncomment if clerkUserId is mandatory
// unique: true, // Uncomment if it must be unique
},
username: {
type: String,
required: true,
// unique: true, // Uncomment if usernames must be unique
},
email: {
type: String,
required: true,
unique: true, // Ensures no two users can share the same email
},
img: {
type: String, // Optional field for storing user profile images
},
savedPosts: {
type: [String], // Array of post IDs the user has saved
default: [], // Default to an empty array
},
},
{ timestamps: true } // Automatically adds createdAt and updatedAt fields
);
export default mongoose.model("User", userSchema);
Beta Was this translation helpful? Give feedback.
All reactions