这是模型:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let locationSchema = new Schema({
'locationTitle': String,
'googlePlaceId': {
'type': String,
'unique': true
},
'coordinates': {
'type': [Number],
'index': '2dsphere'
}, // longitude, latitude
}, {
'timestamps': true
});
mongoose.model('Location', locationSchema);
mongoose.model('Location').collection.createIndex({ 'googlePlaceId': 1 }, { 'unique': 1});
以上是位置的模型 我已经将googlePlaceId设为具有唯一性的索引:true
这是错误:
You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:
2|xplr | MongoError: The field 'retryWrites' is not valid for an index specification. Specification: { name: "googlePlaceId_1", key: { googlePlaceId: 1 }, unique: 1, retryWrites: true }
2|xplr | at /var/xplr/xplr-api/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:581:63
2|xplr | at authenticateStragglers (/var/xplr/xplr-api/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:504:16)
2|xplr | at Connection.messageHandler (/var/xplr/xplr-api/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:540:5)
2|xplr | at emitMessageHandler (/var/xplr/xplr-api/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:310:10)
2|xplr | at TLSSocket.<anonymous> (/var/xplr/xplr-api/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:453:17)
2|xplr | at emitOne (events.js:96:13)
2|xplr | at TLSSocket.emit (events.js:188:7)
2|xplr | at readableAddChunk (_stream_readable.js:176:18)
2|xplr | at TLSSocket.Readable.push (_stream_readable.js:134:10)
2|xplr | at TLSWrap.onread (net.js:559:20)
我试图使retryWrites显式地为假,但仍然给我一个错误。 当我尝试在本地主机上运行我的应用程序时,它可以正常工作。 我不知道为什么在AWS上部署它时会造成问题
您能建议我摆脱这种情况吗?
答案 0 :(得分:0)
很明显,诺言失败没有得到处理,
,原因是,当您尝试插入googlePlaceId不唯一的记录时,由于您的唯一索引,它会引发错误,并且该错误不会在您用代码编写的承诺中得到处理。
因此仅在唯一索引后崩溃,并且在没有唯一索引的情况下可以正常工作。