不能 return collection.findOne() 的值
Can't return the value of collection.findOne()
我正在使用 mongodb 驱动程序 Monk,我想 return 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
});
//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
});
});
});
});
我正在使用 mongodb 驱动程序 Monk,我想 return 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
});
//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
});
});
});
});