Using Mongoose, I can't insert location data into the database :
Can't extract geo keys from object, malformed geometry?
It seems that the POST request is OK (LOG A) : It has a location ('loc') field, but the object I create from this POST request is missing the 'loc' field data :
Here is my Schema :
models/Article.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'});
routes/Article.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)});
});
...
And here are the output :
LOG A : req.body.article
{ title: 'article title',
description: 'about section',
body: 'Article body',
tagList: [],
loc: '{"type":"Point","coordinates":[38.9173025,-77.220978]}'
}
LOG B : article created with var article = new Article(req.body.article);
{ loc: { coordinates: [] },
favoritesCount: 0,
comments: [],
tagList: [],
_id: 5a384f502c9912312c6dd89d,
body: 'Article body',
description: 'about section',
title: 'article title'
}
My problem, is that the loc field began "{ coordinates: [] }" instead of {"type":"Point","coordinates":[38.9173025,-77.220978]} when creating the objet "Article".
Why is that the location data isn't in the Object ?
I've been looking a this question first : Location in mongoose, mongoDB