我正在使用Monk这是一个mongodb驱动程序,我想返回collection.findOne()的值来渲染 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){
});
console.log(mypost);
categories.find({}, {}, function(err, categories) {
for (i=0; i<categories.length; i++) {
mycategories[i] = categories[i];
}
});
res.render('show', {
'pageTitle': mypost.title,
'categories': mycategories,
'post': mypost
});
答案 0 :(得分:0)
//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 is post not my post
console.log(mypost, post);
categories.find({}, {}, function(err, categories) {
for (i=0; i<categories.length; i++) {
mycategories[i] = categories[i];
}
//you cant get access to outside callback
res.render('show', {
'pageTitle': post.title,
'categories': mycategories,
'post': post
});
});
});
});