keyValue is undefined in E11000 duplicate key error

I’m building a node.js server, my problem is that after finishing the front-end part of my web application I wanted to test my error handlers again (I did it first when building the server on Postman), so in the signup form I entered a duplicated username then everything worked as expected but when I tried to signup with a duplicated email the keyValue is undefined for some reasons

here is the error handler in which I printed the error to see what is wrong, when I printed the error I got the data but then it become undefined :

const dbDuplicateKeyHandler = (error) => {
  console.log(error); // ==> print 
                          // index: 0,
                          // code: 11000,
                          // keyPattern: { email: 1 },
                          // keyValue: { email: '' },
                          // statusCode: 500,
                          // status: 'error',
                          // [Symbol(errorLabels)]: Set(0) {}

  console.log(; // ==> print undefined
  // const message = `Duplicated field value: (${}) Please use another value .`;
  return new AppError(400, `Duplicated field value: (${}) Please use another value .`); // ==> is undefined

here is a screenshot of the error when trying with a duplicated email :

The fields you’re pulling out are different than in the example you output unless I’m missing something.

I guess you need to look at what fields are available before you pull it out, as depending on the source of the error (i.e. which field triggered the error) you’ll have a differently shaped error object.

All the fields of the form is filled with data no is missing, in addition there is a validator which check if there is any data is missing from the form before submitting it.

let me explain my problem again, first I entered a duplicated username which is in my case (username), here is the output of these theree lines :

  console.log("THE ERROR IS ==>",error);   
  console.log(" IS ==> ",;
  console.log(" IS ==> ",;


when trying with a new username and duplicated email both of

>  console.log(" IS ==> ",;
>   console.log(" IS ==> ",;

are undefined

Ahh, so when violating two restrictions only one is listed in the error raised?

I’ve not tried violating two restrictions (username and email) yet,
I once just tested the duplicated username (error handling works fine), the second time the turn was for testing just the duplicated email (got undefined for
Do you have any clue where I’ve done it wrong ?

It looks like the shape of the error object changes depending on the error.

When you have a duplicate username you get this:
keyValue: { email: '' }
and with a username you get:
keyValue: { username: 'john' }

The key provided within the keyValue section has the field name and field value, so you cannot have a generic lookup, you need to check what the key is and look at that.

I can’t see the API documentation for that error, maybe someone can link to it if they know where it is.