[英]Node.js app is getting crashed while creating index on AWS EC2 instance but working properly on my local PC
這是模型:
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顯式地為false,但仍然給我一個錯誤。 當我嘗試在本地主機上運行我的應用程序時,它可以正常工作。 我不知道為什么在AWS上部署它時會產生問題
您能建議我采取任何方法擺脫這種情況嗎?
很明顯,諾言失敗沒有得到處理,
這樣做的原因是,當您嘗試插入googlePlaceId不唯一的記錄時,由於您的唯一索引,它會引發錯誤,並且該錯誤不會在您用代碼編寫的承諾中得到處理。
因此,只有在唯一索引之后崩潰,而在沒有唯一索引的情況下才能正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.