当我尝试发布这样的请求
“ {{imgUrl”:“ Server \ Pictures \ i14182109167”,“ text”:“我自己 汉城”,“标记”:[“汉城”,“游览”],“几何”:{“类型”:“点”,“坐标”:[80, -27]}}“
错误原因
“无法提取地理位置键:{_id: ObjectId(\'5b8e204455526366f86a6383 \'),标记:[“ seoul”,“ tour”], 日期:new Date(1536041028423),imgUrl: “服务器\图片\ i14182109167”,文本:“我在首尔”,几何图形:{ 类型:“点”,坐标:[80,-27],_ id: ObjectId(\'5b8e204455526366f86a6384 \')},__v:0}地理元素必须 是数组或对象:类型:“ Point”'}
即使我在帖子请求中添加了“ 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
})
答案 0 :(得分:0)
我更改了几何形状以使其正常工作! 但是,我不知道为什么...
答案 1 :(得分:0)
我遇到了这个错误,尝试了几种模式声明,直到我通过实施此设计解决了这个问题:
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
}
});
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 here
var geolocation =
{
type: 'Point',
coordinates: [
lng,
lat
]
};
const item = new Item({
description: req.body.description,
location: geolocation
})
希望有帮助。