【问题标题】:Multiple callbacks in expressexpress 中的多个回调
【发布时间】: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吗

标签: node.js express callback


【解决方案1】:

据我所知,您做错了一件事:将 next() 置于异步范围之外。

试试这个:

    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) next(err);
            res.locals.blog = allBlogs;
            res.locals.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) return next(err);
            res.locals.review = allReviews;
            res.render("landing", res.locals); // 2nd argument is optional.
        })
    })

【讨论】:

  • 我想要一个专栏用于博客文章,1 个用于评论,类似这样:新闻:[第 1 条],[第 2 条],[第 3 条] 评论:[评论 1],[评论 2 ] , [评论 3]
  • 成功了!非常感谢!那么我们正在做的是将变量存储在 res.locals 中并从那里使用? next() 还需要吗?
  • 是的。我建议你阅读 express 文档:expressjs.com/en/guide/writing-middleware.html
【解决方案2】:

我认为,您做错的是 renderingres.render 相同的页面 2 次,并且每次都传递两个变量之一,这就是为什么另一个变量未定义的原因。

您应该在最后一个callbackrender 页面landing,然后将variables 从第一个callback 传递到第二个。

要将变量从一个回调传递给另一个,您可以使用res.locals

另外,您应该将next() 放在find 的回调中。以便它正确地将变量传递给下一个回调。

试试这个:

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);
            next();
        } else {
            //set variable in `res.locals` and pass to next callback
            res.locals.blog = allBlogs;
            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 {
                    allBlogs = res.locals.blog;
                    res.render("landing", {blog : allBlogs ,review : allReviews, moment : now});
                }   
    })
})

有关 res.locals 的更多信息,请阅读Express res.locals documentation

希望对您有所帮助!

【讨论】:

  • 在第二个回调的 else 中,不应该是:res.locals.review = allReviews; ?如果是这样的话,会不会因为范围问题而定义 allBlogs ?
  • 你好,我看到你已经回答了一些关于 mongodb 的问题。你能帮我这个吗:stackoverflow.com/questions/41898336/mongodb-schema-defining
猜你喜欢
  • 1970-01-01
  • 2013-03-01
  • 1970-01-01
  • 2019-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-02
  • 1970-01-01
相关资源
最近更新 更多