How can i fix (node:14352) UnhandledPromiseRejectionWarning: MongoError: E11000 duplicate key error collection: error? How can i fix (node:14352) UnhandledPromiseRejectionWarning: MongoError: E11000 duplicate key error collection: error? mongoose mongoose

How can i fix (node:14352) UnhandledPromiseRejectionWarning: MongoError: E11000 duplicate key error collection: error?


From the MongoDB BSON guide "The field name _id is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array."

So it would seem you are trying to add 2 documents with the same _id value. The field is automatically indexed to be unique, so if you can be sure that the username is and needs to be unique, you can continue using it, but if there can be multiple same usernames, you should save it to a separate field and leave the _id be.