I have a node/express/mongoose project where I'm not getting something...
I have an endpoint where I'm passing in an object, to populate another object.
The first object may not have all the values needed to populate the new object.
I've tried a number of ways to check each field and if there isn't a matching field in the req.body, then just add a '' to it.
So here is the object I'm trying to build:
var share = {
facebook: {
item: req.body.facebook.item,
_id: req.body.facebook._id
},
linkedin : {
item: req.body.linkedin.item,
_id: req.body.linkedin._id
},
google: {
item: req.body.google.item,
_id: req.body.google._id
}
};
and let's say the object I'm passing in looks like this:
var share = {
facebook: {
item: req.body.facebook.item,
_id: req.body.facebook._id
},
google: {
_id: "5c18929c75727bad4144c0a8"
}
};
As you can see, 'linkedin' is missing all together, and 'google' is missing the 'item' field.
How do I check to see if a field is missing, then populate it with '_blank' fields so I keep the structure of the object the way I need it to be?
Otherwise, I end up with the dreaded: error: "Cannot read property 'item' of undefined" returned.
Thanks!