Reputation: 47
It will update all the values except for the ones in the array, it won't do that .
This is my update
exports.update = function(req, res) {
if (req.body._id) {
delete req.body._id;
}
plan.findById(req.params.id, function(err, plan) {
if (err) {
return handleError(res, err);
}
if (!plan) {
return res.send(404);
}
var updated = _.merge(plan, req.body);
updated.save(function(err) {
if (err) {
return handleError(res, err);
}
return res.json(200, plan);
});
});
};
This is my schema
var ContactSchema = new Schema({
location: {
type: String,
required: true
},
total_cost: {
type: Number,
required: true
},
detailed_expenses: {
transport: {
type: String,
required: true,
trim: true
},
Hotel: {
type: Number,
required: true,
trim: true
},
Food: {
type: Number,
required: true,
trim: true
},
Attractions: {
type: Number,
required: true,
trim: true
}
},
holiday_type: {
type: String,
required: true
},
transport_type: {
type: String,
required: true
},
updated: {
type: Date,
default: Date.now
}
});
Upvotes: 0
Views: 27
Reputation: 459
var updated = _.merge(plan, req.body);
since updated
is simple object not the instance of ContactSchema
hence it wont contain save
function. So you need to create the instance of ContactSchema
using updated
then only you can use save
function on that. Below is your code with update.
exports.update = function(req, res) {
if (req.body._id) {
delete req.body._id;
}
plan.findById(req.params.id, function(err, plan) {
if (err) {
return handleError(res, err);
}
if (!plan) {
return res.send(404);
}
var updated = new plan(req.body); //if req.body is document to be saved
updated.save(function(err) {
if (err) {
return handleError(res, err);
}
return res.json(200, plan);
});
});
};
Upvotes: 1