Node + MongoDB – 错误:process.nextTick(function(){throw err;});

嗨我遇到以下错误,当我运行我的app.js. 无法解决问题。 有人能帮我吗

/Applications/MAMP/htdocs/simplehacker/simplehacker-us/node_modules/mongodb/lib/utils.js:98 process.nextTick(function() { throw err; }); ^ WriteError({"code":11000,"index":0,"errmsg":"E11000 duplicate key error index: instance.instancesmodels.$PublicDnsName_1 dup key: { : \"\" }","op":{"PublicDnsName":"","instanceId":"i-d9ff937a","reservationId":"r-60e814b6","name":"adhoc s1","region":"us-east-1b","status":"stopped","net_io":"","cpu_usage":"","_id":"56a9c6d1e86e8bff040644cb","isInUse":false,"__v":0}}) at Function.MongoError.create (/Applications/MAMP/htdocs/simplehacker/simplehacker-us/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11) at toError (/Applications/MAMP/htdocs/simplehacker/simplehacker-us/node_modules/mongodb/lib/utils.js:114:22) at /Applications/MAMP/htdocs/simplehacker/simplehacker-us/node_modules/mongodb/lib/collection.js:597:23 at handleCallback (/Applications/MAMP/htdocs/simplehacker/simplehacker-us/node_modules/mongodb/lib/utils.js:96:12) at /Applications/MAMP/htdocs/simplehacker/simplehacker-us/node_modules/mongodb/lib/bulk/unordered.js:470:9 at handleCallback (/Applications/MAMP/htdocs/simplehacker/simplehacker-us/node_modules/mongodb/lib/utils.js:96:12) at resultHandler (/Applications/MAMP/htdocs/simplehacker/simplehacker-us/node_modules/mongodb/lib/bulk/unordered.js:417:5) at /Applications/MAMP/htdocs/simplehacker/simplehacker-us/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:780:13 at [object Object].Callbacks.emit (/Applications/MAMP/htdocs/simplehacker/simplehacker-us/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:95:3) at [object Object].messageHandler (/Applications/MAMP/htdocs/simplehacker/simplehacker-us/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:249:23) at Socket.<anonymous> (/Applications/MAMP/htdocs/simplehacker/simplehacker-us/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:265:22) at emitOne (events.js:77:13) at Socket.emit (events.js:169:7) at readableAddChunk (_stream_readable.js:146:16) at Socket.Readable.push (_stream_readable.js:110:10) at TCP.onread (net.js:523:20) 

       

网上收集的解决方案 "Node + MongoDB – 错误:process.nextTick(function(){throw err;});"

答案是在你的第二行:WriteError:

 WriteError({ "code": 11000, "index": 0, "errmsg": "E11000 duplicate key error index: instance.instancesmodels.$PublicDnsName_1 dup key: { : \"\" }", "op": { "PublicDnsName": "", "instanceId": "i-d9ff937a", "reservationId": "r-60e814b6", "name": "adhoc s1", "region": "us-east-1b", "status": "stopped", "net_io": "", "cpu_usage": "", "_id": "56a9c6d1e86e8bff040644cb", "isInUse": false, "__v": 0 } }) 

该索引不允许写入: instance.instancesmodels.$PublicDnsName_1 。 看来你插入重复的PublicDnsName (在你的情况下是一个空string)。

您必须从PublicDnsName索引中删除unique标志,或者允许使用稀疏索引,然后在没有名称时保留空值。

根据您使用的驱动程序,ORM(如mongoose等),可以在应用程序级别或数据库级别解决此问题。