mongodb - Can't return the value of collection.findOne() -
i'm using monk mongodb driver, , want return value of collection.findone() render mypost response parameter.
this code:
//show single post router.get('/show/:id', function(req, res, next){ var mycategories = [] var mypost = {}; var posts = db.get('posts'); var categories = db.get('categories'); posts.findone({_id: req.params.id}, function(err, post){ }); console.log(mypost); categories.find({}, {}, function(err, categories) { (i=0; i<categories.length; i++) { mycategories[i] = categories[i]; } }); res.render('show', { 'pagetitle': mypost.title, 'categories': mycategories, 'post': mypost });
//show single post router.get('/show/:id', function(req, res, next){ var mycategories = [] var mypost = {}; var posts = db.get('posts'); var categories = db.get('categories'); posts.findone({_id: req.params.id}, function(err, post){ //success param post not post console.log(mypost, post); categories.find({}, {}, function(err, categories) { (i=0; i<categories.length; i++) { mycategories[i] = categories[i]; } //you cant access outside callback res.render('show', { 'pagetitle': post.title, 'categories': mycategories, 'post': post }); }); }); });
Comments
Post a Comment