在Express中使用多个MongoDB查询进行异步/等待

时间:2018-12-16 18:08:40

标签: node.js mongodb express asynchronous async-await

我有一个相当简单的CRUD应用程序,它将两个查询的结果呈现到一页上。一旦使它“起作用”,就会出现问题是该页面需要刷新才能显示结果。首次加载时,未显示任何结果。

我发现这是Node异步性质的问题/症状。我一直在尝试通过使用async / await来解决此问题,并且经过数小时的混乱工作后,我觉得我已经很接近解决方案了,但是还没有解决-我仍然需要手动刷新才能显示/在.ejs页面上呈现结果。

代码:

var entries = [];
var frontPageGoals = [];

app.get('/entries', async (req,res) => {
  if (req.session.password) {

      const entriesColl = await 
            db.collection('entries')
            .find()
            .sort({date: -1})
            .toArray((err, result) => {
               if (err) { console.log(err) }
               else {
                  for (i=0; i<result.length; i++) {
                       entries[i] = result[i];
                  }
               }
            });

      const goalsColl = await 
            db.collection('goals')
            .find()
            .toArray((err, result) => {
               if (err) {console.log(err)}
               else {
                  for (i=0; i<result.length; i++) {
                       frontPageGoals[i] = result[i];
                  }
               }
            });

      res.render('index.ejs', {entries: entries, frontPageGoals: frontPageGoals});
   }
   else {
      res.redirect('/');
   }
 });

现在,我可以在这里想到一些问题,但是老实说,我只是想解决这个问题。例如,我确定在页面渲染超出实际异步功能时,包含将要传递的结果的空列表是有问题的。但是在尝试将它们移动到异步区域内的十二个不同位置之后……仍然没有骰子。

任何帮助将不胜感激!这基本上是我需要为此应用完成的最后一项“大任务”。

1 个答案:

答案 0 :(得分:0)

我不确定您的数据库驱动程序是100%可靠的,但是假设toArray()返回了一个Promise(在默认的mongodb驱动程序中是这样),await实际上将返回您的值期望在您的回调中使用result,或者如果发生错误(您期望在回调中将其作为err),则会抛出该错误,从而迫使您使用{{1} }块,在这种情况下,您只需在catch块中使用try-catch,因为您没有进行任何处理

这是更新后的代码:

console.log(err)

编辑

但是,如果您不了解诺言-app.get("/entries", async (req, res) => { if (req.session.password) { try { const entries = await db .collection("entries") .find() .sort({ date: -1 }) .toArray(); const frontPageGoals = await db .collection("goals") .find() .toArray(); res.render("index.ejs", { entries: entries, frontPageGoals: frontPageGoals }); } catch (err) { console.log(err); } } else { res.redirect("/"); } }); 本质上是诺言-,并且只想使用回调-不建议-,那么您只需发送您在回调中的响应,并将第二个查询嵌套在第一个查询的回调中,这是代码,其中包含一些注释,希望可以帮助您:

async/await
  • 由于您不再需要app.get("/entries", (req, res) => { if (req.session.password) { // First query db.collection("entries") .find() .sort({ date: -1 }) .toArray((err, entryResult) => { if (err) { console.log(err); } else { // In the callback of the first query, so it will // execute 2nd query, only when the first one is done db.collection("goals") .find() .toArray((err, frontPageResult) => { if (err) { console.log(err); } else { // In the callback of the 2nd query, send the response // here since both data are at hand res.render("index.ejs", { entries: entryResult, frontPageGoals: frontPageResult }); } }); } }); } else { res.redirect("/"); } }); 关键字,因此我将其删除了
  • 我重命名了回调参数,而不仅仅是async,因为两个回调将具有相同的参数名称,并且您必须将其存储在temp变量中