请参考这个问题这里
我有同样的问题,除了我没有提供任何地理数据。
下面是我的模型的地理数据实现部分
startLocation: {
// GeoJSON
type: {
type: String,
default: 'Point',
enum: ['Point']
},
coordinates: [Number],
address: String,
description: String
},
locations: [
{
type: {
type: String,
default: 'Point',
enum: ['Point']
},
coordinates: [Number],
address: String,
description: String,
day: Number
}
]
以下每个案例的相同请求正文
{
"name": "The Test Tourxx",
"duration": 1,
"maxGroupSize": 1,
"difficulty": "medium",
"price": 501,
"summary": "Nothing exciting here man!",
"imageCover": "tour-3-cover.jpg",
"ratingsAverage": 4,
"guides": ["5e85e24bd1b3f918dc07bb3d", "5e85e262d1b3f918dc07bb3e"]
}
下面连接本地MongoDB时的响应
{
"status": "error",
"error": {
"driver": true,
"name": "MongoError",
"index": 0,
"code": 16755,
"errmsg": "Can't extract geo keys: { _id: ObjectId('5e85fc64a4031f1734b55515'), startLocation: { type: \"Point\", coordinates: [] }, ratingsAverage: 4, ratingsQuantity: 0, rating: 4.5, images: [], createdAt: new Date(1585839200315), startDates: [], secretTour: false, guides: [ ObjectId('5e85e24bd1b3f918dc07bb3d'), ObjectId('5e85e262d1b3f918dc07bb3e') ], name: \"The Test Tourxx\", duration: 1, maxGroupSize: 1, difficulty: \"medium\", price: 501, summary: \"Nothing exciting here man!\", imageCover: \"tour-3-cover.jpg\", locations: [], slug: \"the-test-tourxx\", __v: 0 } Point must only contain numeric elements",
"statusCode": 500,
"status": "error"
},
"stack": "MongoError: Can't extract geo keys: { _id: ObjectId('5e85fc64a4031f1734b55515'), startLocation: { type: \"Point\", coordinates: [] }, ratingsAverage: 4, ratingsQuantity: 0, rating: 4.5, images: [], createdAt: new Date(1585839200315), startDates: [], secretTour: false, guides: [ ObjectId('5e85e24bd1b3f918dc07bb3d'), ObjectId('5e85e262d1b3f918dc07bb3e') ], name: \"The Test Tourxx\", duration: 1, maxGroupSize: 1, difficulty: \"medium\", price: 501, summary: \"Nothing exciting here man!\", imageCover: \"tour-3-cover.jpg\", locations: [], slug: \"the-test-tourxx\", __v: 0 } Point must only contain numeric elements\n at Function.create (C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\core\\error.js:43:12)\n at toError (C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\utils.js:149:22)\n at C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\operations\\common_functions.js:265:39\n at handler (C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\core\\sdam\\topology.js:913:24)\n at C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\cmap\\connection_pool.js:352:13\n at handleOperationResult (C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\core\\sdam\\server.js:487:5)\n at MessageStream.messageHandler (C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\cmap\\connection.js:270:5)\n at MessageStream.emit (events.js:311:20)\n at processIncomingData (C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\cmap\\message_stream.js:144:12)\n at MessageStream._write (C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\cmap\\message_stream.js:42:5)\n at doWrite (_stream_writable.js:441:12)\n at writeOrBuffer (_stream_writable.js:425:5)\n at MessageStream.Writable.write (_stream_writable.js:316:11)\n at Socket.ondata (_stream_readable.js:714:22)\n at Socket.emit (events.js:311:20)\n at addChunk (_stream_readable.js:294:12)"
}
连接到下面的 MongoDB Atlas 时的响应(已删除无关药水)
{
"status": "success",
"data": {
"tour": {
"startLocation": {
"type": "Point",
"coordinates": []
},
"ratingsAverage": 4,
"images": [],
"guides": [
"5e85e24bd1b3f918dc07bb3d",
"5e85e262d1b3f918dc07bb3e"
],
"_id": "5e85fb952754013728fc950c",
"name": "The Test Tourxx",
"duration": 1,
"maxGroupSize": 1,
"difficulty": "medium",
"price": 501,
"summary": "Nothing exciting here man!",
"imageCover": "tour-3-cover.jpg",
"locations": [],
"__v": 0,
}
}
}
可以看出,本地数据库在为“位置”创建空数组时会引发错误,但 Atlas 做得很好。请帮我找出原因。
Mongoose:5.9.7 Mockgoose:未在项目中使用 MongoDB:4.2.3(本地和 Atlas 两者)