I have a problem when I want to update my nested objects in express. Here is my code:
.put(function(req, res) {
Place.findById(req.params.place_id, function(err, place) {
if (err)
res.send(err);
if (typeof req.body.zip !== 'undefined') {
place.zip = req.body.zip;
}
if (typeof req.body.loc !== 'undefined') {
place.loc = req.body.loc;
}
if (typeof req.body.coordinates !== 'undefined') {
place.coordinates = req.body.coordinates;
}
place.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Place updated!' });
});
});
});
It is working, when I want to update the zip, but I cannot modify the coordinates. I tried also the place.loc.coordinates. I am using curl to update, maybe that command the wrong one. I tried the
curl -X PUT -d loc.coordinates=[1.3,3.2] 'http://localhost:8080/api/places/A38'
curl -X PUT -d coordinates=[1.3,3.2] 'http://localhost:8080/api/places/A38'
commands too.
And my schema is:
var placeSchema = new Schema({
_id: String,
zip: Number,
loc: {
type: { type: String }
, coordinates: []
}
});
Could anyone help me please?
I tried both of your ideas, but unfortunately those are not worked. I found a solution, maybe it is not elegant, but works: I changed my schema to:
var placeSchema = new Schema({
_id: String,
zip: Number,
loc: {
type: Object
, index: '2dsphere'
}
});
And my new code is:
if (typeof req.body.coordinates !== 'undefined') {
place.loc = { type: 'Point', coordinates: req.body.coordinates };
}