Express 在使用ejs查看的路线中快速传递值

Express 在使用ejs查看的路线中快速传递值,express,ejs,Express,Ejs,这是我的邮寄路线: // CRUD posts. app.post('/crud', function(req, res) { db.users.addPost(req.body); res.redirect('crud') }); 这是我的获取路线: app.get('/crud', function(req, res) { res.render('crud', { user: req.user, posts: db.users.retrievePost()

这是我的邮寄路线:

// CRUD posts.
app.post('/crud', function(req, res) {
  db.users.addPost(req.body);
  res.redirect('crud')
});
这是我的获取路线:

app.get('/crud', function(req, res) {
  res.render('crud', {
    user: req.user,
    posts: db.users.retrievePost()
  });
});
这是我用来从数据库检索帖子的函数:

// Retrieve posts from database to pass to view.
exports.retrievePost = function() {
  var cursor = postsCollection.find().toArray(function(err, record) {
    console.log(record);
    return record;
  });
}
记录日志时,它返回以下数据:

[ 
  { _id: 57cb835ba8b8250bdcd65e5d, userCreate: 'This is a question' },
  { _id: 57cb85709435720c055b10e6, userCreate: 'hello' },
  { _id: 57cb87b10dc3ec0c3a78f970, userCreate: 'hello' },
  { _id: 57cb87c20dc3ec0c3a78f971, userCreate: 'what up here' } 
]
这就是我使用ejs在“crud”视图中显示userCreate值的方式

<ul class="posts">
  <% for(var i=0; i<posts.length; i++) { %>
  <li class="post">
    <span><%= posts[i].userCreate %></span>
  </li>
  <% } %>
</ul>
并且能够以以下方式渲染它:

<%= title %>


所以我真的不知道为什么posts不能工作。

正如我在评论中所描述的:问题是传递给
res.render
posts
的值没有定义。
postsCollection.find()
将不会返回,并且该值在任何情况下都不会传回。您还需要让
retrievePost
接受回调(或者用承诺等方式)

这是所需行为的一个非常基本的实现

app.get('/crud', function(req, res) {
    // call passing in a callback
    db.users.retrievePost(function(err,result) {
        // call render inside this call back, after result has been retrieved
        res.render('crud', {user: req, user, posts: result});
    })
});

// Retrieve posts from database to pass to view.

exports.retrievePost = function(callback) {
   var cursor = postsCollection.find().toArray(function(err, record) {
      console.log(record);
      // naively just invokes the passed callback
      // you could also do error handling or other processing
      // here as well, before passing back the result through the callback
      callback(err,record);
   });
}
它将是:

locals.posts.length()

而不是


posts.length

因为传递给
res.render
posts
的值未定义。
postsCollection.find()
将不会返回,并且该值在任何情况下都不会传回。你需要让
retrievePost
也接受回调(或者用承诺来做),我想我明白你的意思了。与游标关联的匿名函数返回记录,但retrievePost()不返回。你是这么说的吗?如果是这样,我认为你是对的。那么,我该如何让retrievePost()返回该数据,而不是其中的匿名函数呢。我必须研究这个功能。我有点搞不清楚数据是如何从导出的函数返回到函数参数,然后返回到参数的来源。@manbearpig1它的函数一直向下。您正在调用函数并传入作为函数的参数。当被调用函数完成时,它从参数(调用调用函数中定义的代码)调用函数。如果你不想太多的话,就不会那么困惑了。:-)基本上,它是处理异步性的众多策略之一。回调是处理它的最古老的常用方法,但我也建议您研究承诺,尽量少使用JS,但可能使用像Bluebird这样的库。
app.get('/crud', function(req, res) {
    // call passing in a callback
    db.users.retrievePost(function(err,result) {
        // call render inside this call back, after result has been retrieved
        res.render('crud', {user: req, user, posts: result});
    })
});

// Retrieve posts from database to pass to view.

exports.retrievePost = function(callback) {
   var cursor = postsCollection.find().toArray(function(err, record) {
      console.log(record);
      // naively just invokes the passed callback
      // you could also do error handling or other processing
      // here as well, before passing back the result through the callback
      callback(err,record);
   });
}