Node.js ejs模板中的Mongodb游标为空(ejs内的异步调用)

Node.js ejs模板中的Mongodb游标为空(ejs内的异步调用),node.js,mongodb,express,ejs,Node.js,Mongodb,Express,Ejs,下面的路由器调用ejs模板,将游标的值填充到html页面中 router.get('/users_loaded_disconnect', function(req, res) { res.render('users_loaded_disconnect', {cursor: req.db.collection('notify_user_state_collection').find({})}); }); 用户\u加载\u断开连接。ejs <!DOCTYPE h

下面的路由器调用ejs模板,将游标的值填充到html页面中

router.get('/users_loaded_disconnect', function(req, res) {
    res.render('users_loaded_disconnect',
           {cursor: req.db.collection('notify_user_state_collection').find({})});
});
用户\u加载\u断开连接。ejs

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <b> Users Loaded Disconnect </b>
    <ul>
      <% cursor.nextObject(function(err, item) { %>
      <%= JSON.stringify(item) %>
      <% }); %>
    </ul>
  </body>
</html>

在ejs模板内迭代游标有什么问题?

游标操作是异步的。Ejs不会等待它完成,而是在数据可用之前继续呈现模板。您无法在ejs模板中有效地使用回调

req.db.collection('notify_user_state_collection').find({}).nextObject(function(err, item) {
        console.log(JSON.stringify(item));
    });