【发布时间】:2017-01-21 06:19:10
【问题描述】:
我有 2 个架构:Blogdemo 和 Review。两者都在同一个文件中:landing.ejs 我希望两种模式的内容都出现在登录页面上。
代码:
app.get('/', function (req, res,next) {
Blogdemo.find({}).sort([['_id', -1]]).limit(3).exec(function(err,allBlogs) { //finds the latest blog posts (upto 3)
if(err) {
console.log(err);
} else {
res.render("landing", {blog : allBlogs , moment : now});
}
})
next();
}, function (req, res) {
Review.find({}).sort([['_id', -1]]).limit(3).exec(function(err,allReviews) { //finds the latest reviews (upto 3)
if(err) {
console.log(err);
} else {
res.render("landing", {review : allReviews, moment : now});
}
})
})
我收到错误消息:“未定义审核”。如果我更改回调的顺序,我会收到错误消息:“博客未定义”。我知道回调有问题。
我浏览了 express 文档并使用了这个:
app.get('/', function (req, res, next) {
console.log('Request URL:', req.originalUrl)
next()
}, function (req, res, next) {
console.log('Request Type:', req.method)
next()
})
这很好用。但我遵循确切的模式,但它不起作用。我做错了什么?
【问题讨论】:
-
@MukeshSoni 4.14.0
-
尝试在上述回调中的 else 块之后立即移动 next()
-
@AsifSaeed 没用。
-
只是为了尝试移动Review.find 块旁边的res.render("landing", {blog : allBlogs , moment : now});
-
你能粘贴你的landing.ejs吗