I often encounter problems in terms of design if it comes to this point:
var pages,
book
;
Book.findOne( { title: "First Book" }, function(err, doc) {
pages = doc.pages;
console.log( pages );
} );
res.json(pages, 200);
pages is undefined, though I set it to doc.pages. If I move the res.json(pages, 200) into the callback of findOne this script works perfectly.
Am I following a wrong pattern/code design when it comes to JavaScript or is there a solution to keep the res.json() out of the callback, apart from making pages global?