So I have a number of Mongoose models in my web app. I want to be able to set the fields as empty when the payload of a PUT request contains that data as empty. I am not sure what i have written above makes much sense so ill try to explain through the code I have.
if(req.params.iId){
Industry.findById(req.params.iId, function(err, industry){
if (err){
return res.send(500, {
message: getErrorMessage(err)
});
}
if (!industry){
return res.send(204, {
message: 'No industry found, Cannot Update'
});
}
industry.industry = req.param('name');
industry.updated = Date.now();
industry.save(function(err, newIndustry) {
if (err) {
return res.send(500, {
message: getErrorMessage(err)
});
} else {
newIndustry.__v = undefined;
newIndustry.withEffectTo = undefined;
newIndustry.withEffectFrom = undefined;
if(isResponseJson){
res.json(industry);
}else{
res.jsonp(industry);
}
}
});
});
}
From the client side a send a JSON payload of the form:
{
"industry":""
}
Through this payload, I intend to set the particular Industry objects industry field to empty, not caring about whatever value of previously had. The problem is when I send this payload it causes no change and the object keeps whatever value it previously had. I know that setting that particular value to undefined and then saving it would do the trick (another related question)
but i have a number of request parameters in other models and how do i set there fields as empty when a corresponding empty value for their key is obtained from the request payload. Any help would be really appreciated.