我最近遇到了这个 mongo 异常,当我打印错误对象时,它没有提到我的代码中出现异常的位置。
{ 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)]: {} }
正如你所看到的,它只提到了 mongodb 模块中的哪些文件出现了,但现在我自己的项目代码中的哪些行启动了整个事件链。也许这是我使用的一些 .save() 函数?为什么它不显示这条线,是否可以在任何地方看到它?这对我的调试工作确实有帮助
最佳答案
因为 MongoDB 不是您应用程序的一部分,它是您添加的模块,并且您使用的驱动程序直接传递 MongoDB 错误。
通常程序员处理驱动程序返回的错误对象如下:
UsersModel.save(function(err) {
if (err) {
// Evaluate the error over here
}
})
请注意,上面的代码表示使用 Mongoose 模块保存一条新记录。
祝你好运..
关于javascript - 为什么 MongoError : E11000 exception message not informing me where it was risen?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57622557/