使用mongoose,我无法将位置数据插入数据库:
Can't extract geo keys from object, malformed geometry?
看来,POST请求还可以(LOG A):它具有一个位置('loc')字段,但是我从该帖子请求创建的对象缺少" LOC"字段数据:
这是我的模式:
Models/Artend.js
var ArticleSchema = new mongoose.Schema({
slug: {type: String, lowercase: true, unique: true},
title: String,
description: String,
body: String,
loc : { type: {type:String}, coordinates: [Number]},
favoritesCount: {type: Number, default: 0},
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],
tagList: [{ type: String }],
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
}, {timestamps: true});
ArticleSchema.index({loc: '2dsphere'});
路由/Artend.js
router.post('/', auth.required, function(req, res, next) {
User.findById(req.payload.id).then(function(user){
if (!user) { return res.sendStatus(401); }
var article = new Article(req.body.article);
// LOG A
console.log(req.body.article);
// LOG B
console.log(article);
article.author = user;
return article.save().then(function(){
return res.json({article: article.toJSONFor(user)});
});
...
这是输出:
log a:req.body.tricle
{ title: 'article title',
description: 'about section',
body: 'Article body',
tagList: [],
loc: '{"type":"Point","coordinates":[38.9173025,-77.220978]}'
}
log B:用 var rats =新文章(req.body.ticle)创建的文章;
{ loc: { coordinates: [] },
favoritesCount: 0,
comments: [],
tagList: [],
_id: 5a384f502c9912312c6dd89d,
body: 'Article body',
description: 'about section',
title: 'article title'
}
我的问题是,LOC字段开始" {坐标:[]}" ,而不是 {" type':" point"," coortinates":[38.9173025,-77.22097888]} 创建objet"文章"。
为什么位置数据不在对象中?
我首先一直在寻找一个问题:位于Mongoose,Mongodb
感谢@veeram:问题是我的帖子请求:该位置是作为字符串而不是对象发送的:
在我的角前端:
Article.Model.ts
export class Article {
....
loc: string
....
}
Article.Model.ts之后
class Location {
constructor(
public type: string,
public coordinates: Array<number> = []
){}
}
export class Article {
....
loc: Location
....
}