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

时间:2016-01-28 08:05:58

标签: node.js mongodb

您好我在运行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)

1 个答案:

答案 0 :(得分:2)

答案在第二行: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(在您的情况下是一个空字符串)。

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

根据您使用的驱动程序,ORM(例如猫鼬等),您可以在应用程序级别或数据库级别解析它。