我想将loc数据发布到postman / form-data中,而没有application / json头。
{
"text": "abcd",
"loc": {
"type": "Point",
"coordinates": [-80, 25]
}
"img": multipart
}
当我在postman / form-data中整理这个内容
loc.coordinates [0]:-80 loc.coordinates [1]:25文本:... img:...
loc.coordinates不返回任何内容,如下所示。
{
"data": {
"text": "test",
"tags": [
"testtest"
],
"_id": "5b9e1462c1f13a0fe0912933",
"img": "https://seoul-image-server.s3.cp.ap-northeast-2.amazonaws.com/1537086562695.png",
"user": "5b8e4249f9f8184d0c825c6a",
"date": "2018-09-16T08:29:22.791Z",
"__v": 0
}
}
当我在表单数据中感到厌倦时
loc:{“ type”:“ Point”,“ coordinates”:[-80,25]} ...
返回
"message": "Memo validation failed: loc: Cast to Embedded failed for value \"{\"type\": \"Point\", \"coordinates\": [-80, 25]}\" at path \"loc\""
为什么?
这是我的架构
const geoSchema = new Schema({
type: {
type: String,
default: 'Point'
},
coordinates: {
required: true,
type: [Number]
}
});
const memoSchema = new Schema({
img: {
type: String,
required: true
},
text: {
type: String,
default: ""
},
date: {
type: Date,
default: Date.now
},
tags: {
type: [String],
set: item => {
if(Array.isArray(item)) {
return item.join('')
}
},
defulat: []
},
user: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
loc: geoSchema
});
memoSchema.index({
loc: '2dsphere'
});
module.exports = mongoose.model('Memo', memoSchema)
答案 0 :(得分:1)
您正在发送JSON,这意味着您必须将数组包装在双引号“ coordinates”中:“ [-80,25]”