I would like to only update a value in the document if its newly defined. If its not defined, leave the value as is in the document. Example below.
If this document exists in the db
{
"_id": "592c53b3bdf350ce004ad717",
"url": "hyoh7ryb-",
"fname": "Foo",
"lname": "Bar",
"email": "foobar@gmail.com",
"__v": 0,
"verfied": false,
"bio": {
"title": "Software Engineer",
"intro": "I like to code stuff.",
"summary": "Specializing in web development. Favorite language is Javascript.\n"
}
}
My code:
module.exports.updateBio = function(req, res) {
var userID = req.params.id;
var objForUpdate = {bio: {}};
if (!troolr.isEmptyString(req.body.profile_picture)) objForUpdate.bio.profile_picture = req.body.profile_picture;
if (!troolr.isEmptyString(req.body.title)) objForUpdate.bio.title = req.body.title;
if (!troolr.isEmptyString(req.body.intro)) objForUpdate.bio.intro = req.body.intro;
if (!troolr.isEmptyString(req.body.summary)) objForUpdate.bio.summary = req.body.summary;
if (!troolr.isEmptyString(req.body.skills)) objForUpdate.bio.skills = req.body.skills;
if (!troolr.isEmptyString(req.body.facebook)) objForUpdate.bio.social.facebook = req.body.facebook;
if (!troolr.isEmptyString(req.body.twitter)) objForUpdate.bio.social.twitter = req.body.twitter;
if (!troolr.isEmptyString(req.body.linkedin)) objForUpdate.bio.social.linkedin = req.body.linkedin;
if (!troolr.isEmptyString(req.body.website)) objForUpdate.bio.social.website = req.body.website;
var conditions = { _id: userID }
, setObj = { $set: objForUpdate }
, options = { multi: true };
Profile.update(conditions, setObj, (error, page) =>{
if(error) return res.status(500).json({ success: false, error: error });
res.status(200).json({ success: true, message: "Bio successfully updated." });
});
};
In my body send request, i am only updating title, but i would want to keep all the other bio info as is, but instead it creates a blank field for them.
How can I go about handling this?
via Shivam
No comments:
Post a Comment