I am probably overlooking the most simple solution but I've tried anything I could come up with or find on the internet.
I'm trying to add a property to my JSON object which I need in my output. But whatever I try to add the property, it doesn't show up in my object. I verified that the 'doc' variable is an actual object.
No console errors. Console.log() shows the contents of 'doc' object and nothing more.
Anyone knows what is going on?
Blog.findOne({'slug': req.params.slug}, function(err, doc) {
if (!err){
if(_.isEmpty(doc)) { res.status(404).json({ response: "Nothing found here." }); } else {
var blogPost = doc;
blogPost.someProperty = 'test';
console.log(blogPost); // doesn't contain new property
res.status(200).json( { response: blogPost });
}
} else { throw err; }
});
Output of blogPost/doc
{
"response": {
"_id": "55e31854823389ec1f5506fc",
"title": "A sample post.",
"slug": "a-sample-post2",
"body": "Hello world. This is a sample post. \n\nThis should be a new paragraph.",
"__v": 0,
"date": "2015-08-30T14:51:00.836Z"
}
}
A simple test like this in the browser does work. So I am really confused why it doesn't work in my NodeJS application. Something to do with callbacks/async or something?
var test = {};
test.prop = "test";
console.log(test);
Object {prop: "test"}