0

I got this mongo exception recently, and when i printed my error object it did not mention where in my code that exception was risen.

{ MongoError: E11000 duplicate key error collection: test.users index: email_1 dup key: { : "test@gmail.com" }
[0]     at Function.create (C:\proj\node_modules\mongodb-core\lib\error.js:43:12)
[0]     at toError (C:\proj\node_modules\mongodb\lib\utils.js:149:22)
[0]     at coll.s.topology.insert (C:\proj\node_modules\mongodb\lib\operations\collection_ops.js:859:39)
[0]     at handler (C:\proj\node_modules\mongodb-core\lib\topologies\replset.js:1155:22)
[0]     at C:\proj\node_modules\mongodb-core\lib\connection\pool.js:397:18
[0]     at process._tickCallback (internal/process/next_tick.js:61:11)
[0]   driver: true,
[0]   name: 'MongoError',
[0]   index: 0,
[0]   code: 11000,
[0]   errmsg:
[0]    'E11000 duplicate key error collection: test.users index: email_1 dup key: { : "test@gmail.com" }',
[0]   [Symbol(mongoErrorContextSymbol)]: {} }

As you can see, it only mentions in what files in the mongodb module it had risen, but now which lines in my own project code had started this entire chain of events. Perhaps it's some .save() function that I used? Why doesn't it show this line, and is it possible to see it anywhere? that would really help with my debugging efforts

Reporter
  • 3,897
  • 5
  • 33
  • 47
user2435678
  • 319
  • 2
  • 3
  • 14
  • 1
    A shot into the dark: `MongoError: E11000 duplicate key error collection: test.users index: email_1 dup key:`says what is wrong. – Reporter Aug 23 '19 at 08:41
  • Thank you, yes this is true. But still, it would be helpful for me to know in what line in my code it originated, is it possible to get this information? – user2435678 Aug 23 '19 at 08:47
  • You haven't posted your code so it is impossible to give you an appropiate answer. – Reporter Aug 23 '19 at 08:50
  • @user2435678. It is not a problem in the code. It is the problem with the data you are sending. To be more clear the the collection already has test@gmail.com email in the same field. That's why it is not accepting. If you send another email which does not stored in the database then It will work fine. This is happening due to creation of unique index on email field. – Amaranadh Meda Aug 23 '19 at 08:51
  • @AmaranadhMeda I know, but there must be a code line that initiated this attempt to save in the DB for an already existing email address, and it would be helpful for me to know which line in my code it happened in. – user2435678 Aug 23 '19 at 09:20
  • @user2435678 why don't you post any relevant code? – Reporter Aug 23 '19 at 14:48

1 Answers1

1

Because MongoDB is not a part of your application, it is a module you added and the driver you use, passes MongoDB errors directly.

Generally programmers handles error objects that are returned by the driver just like as following:

UsersModel.save(function(err) {
    if (err) {
        // Evaluate the error over here
    }
})

Note that the code above represents of saving a new record by using Mongoose module.

Good luck..

efkan
  • 12,991
  • 6
  • 73
  • 106
  • Thank you, but in the error handler function you showed above, is it possible to log the error in my console along with the line in my express code in which the exception originated (as in, where that .save function is located in my code) – user2435678 Aug 23 '19 at 09:22
  • If I understood correctly, the error did not occur in your app. It is regarding your MongoDB request and occured on db server. So, your console shows a decent error message:` collection: test.users index: email_1 dup key: { : "test@gmail.com" }` – efkan Aug 23 '19 at 11:10
  • how could that be though? As far as I know, all the HTTP packets are connected directly to my express routers which in turn call the mongodb. I don't think there's a direct communication between client to mongo, so it must go through my app code. However, I'm very new to express & react & mongo so perhaps I'm missing something? – user2435678 Aug 23 '19 at 11:42
  • 1
    Actually you are right. Everything is clear. I just meant that a request is made in express app, wherever. MingoDB module (driver) is used for to move the request to MongoDB. MongoDB returns a duplicate key error. This error is handled in MongoDB driver and the error is thrown. However the error output above, contains only 5 lines of error stack. Because of that the error doesn't contain the related code line you wrote. If error message has 10 lines from stack, maybe you can see which code you wrote caused the error. By writing an error handler we can see more lines from error stack.. – efkan Aug 23 '19 at 14:20
  • Check this out: https://stackoverflow.com/questions/7697038/more-than-10-lines-in-a-node-js-stack-error – efkan Aug 23 '19 at 14:23