当我尝试像这样发布请求的时候
{ "imgUrl":“服务器\图片\i 14182109167”,“文本”:“我在首尔”,“标签”:“汉城”,“旅游”,“几何”:{“类型”:“点”,“坐标”:80,-27}
错误原因
‘无法提取地理键:{ _id: ObjectId(’5b8e2044526366f86a6383‘),标签:“汉城”,“旅游”,日期:新日期(1536041028423),imgUrl:“服务器\图片\i 14182109167”,文本:“我在首尔”,几何学:{ type:"Point",坐标: 80,-27,_id: ObjectId(\’5b8e2044555266f86a6384) },__v: 0} geo元素必须是数组或对象:类型:“Point”}
甚至我也在post请求中添加了"type":"Point“,但是,为什么?
const geoSchema = new Schema({
type: {
type: String,
default: 'Point',
index: '2dsphere'
},
coordinates: {
type: [Number]
}
});
const memoSchema = new Schema({
imgUrl: {
type: String
},
text: {
type: String
},
date: {
type: Date,
default: Date.now
},
tag: {
type: [String]
},
user: {
type: Schema.Types.ObjectId,
ref: 'Memo'
},
geometry: geoSchema
})发布于 2019-11-17 14:14:23
我经历了这个错误,尝试了几个模式声明,直到我通过实现这个设计来修复:
1.创建一个新模式,它将Point作为属性。
const mongoose = require('mongoose');
const {Point} = require('mongoose-geojson-schema');
const pointSchema = new mongoose.Schema({
type: {
type: String,
enum: ['Point'],
required: true
},
coordinates: {
type: [Number],
required: true
}
});2.对象的架构,包括上述属性:
const itemsSchema = new mongoose.Schema({
description: {
type: String,
required: true
},
location: {
type: pointSchema,
required: true
}
)
const Item = mongoose.model('Item', ItemsSchema);
module.exports = Item;
enter code here3.最后,我的控制器成功地实例化了对象,如下所示:
var geolocation =
{
type: 'Point',
coordinates: [
lng,
lat
]
};
const item = new Item({
description: req.body.description,
location: geolocation
})希望这能有所帮助。
发布于 2018-09-04 12:00:38
我改变了几何学,让它成功了!但是我不知道为什么..。
发布于 2022-08-19 08:30:39
即使在做了这里提到的所有事情之后,我也面临着同样的问题。在这个问题上花了太多时间之后,我知道如果我们保存一个默认值,它将正确工作。
只需按照上面的答案添加
location: {
type: pointSchema,
default: {
type: 'Point',
coordinates: [0, 0],
},
required: false,
},如果任何文档都有位置,则删除以前的文档。
https://stackoverflow.com/questions/52159814
复制相似问题