Node.js 竞争条件发送数据到 ejs 文件
Node.js race condition sending data to ejs file
我正在尝试从我的 mongodb 服务器获取实验室数据。我能够获取数据,因为我可以控制台记录它,但是我觉得我遇到了一个竞争条件,其中 res.render 发生在实际的数据库调用之前。现在我尝试嵌套调用,以便在我获取数据后进行渲染,如下所示:
Lab.find()
.then( (result)=>{
console.log(result)
res.render('main/labs', { results : result});
}).catch((err)=>{
console.log(err);
res.status(500).render('main/error')
})
});
然而,我总是以同样的错误告终,所以我不确定我是否正确地嵌套了函数调用,或者我忽略了其他东西。
{
_id: 607fc39492b8a4ada9aec28a,
title: 'Lab 01',
description: 'Highlight Table Cells',
filename: '01_lab'
}
]
[2021-04-21T02:51:34.962] [INFO] default FROM=> http://localhost:3000/home, TO=> GET /labs, STATUS=> 500
TypeError: /home/christien/Desktop/web-development/JavaScript_Demos/Labs/resources/main/labs.ejs:15
13| <div class="card" style="width: 15rem;">
14| <div class="card-body">
>> 15| <h5 class="card-title"><%= results[i].title %></h5>
16| <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
17| <p class="card-text"><%=results[i]%></p>
18| <a href="#" class="card-link">Go To Lab</a>
请帮忙!
编辑:
这是我要加载的 ejs 文件
<h3>Labs</h3>
<p>The following are labs that have been completed</p>
<div class="d-flex justify-content-sm-between align-items-center">
<% for (var i =0; i <= results.length; i++ ) { %>
<div class="card" style="width: 15rem;">
<div class="card-body">
<h5 class="card-title"><%= results[i].title %></h5>
<h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
<p class="card-text">Description</p>
<a href="#" class="card-link">Go To Lab</a>
</div>
</div>
<% } %>
</div>
</div>
将查询更改为以下内容
Lab.find().exec().then((results) => {
// handle results
})
我正在尝试从我的 mongodb 服务器获取实验室数据。我能够获取数据,因为我可以控制台记录它,但是我觉得我遇到了一个竞争条件,其中 res.render 发生在实际的数据库调用之前。现在我尝试嵌套调用,以便在我获取数据后进行渲染,如下所示:
Lab.find()
.then( (result)=>{
console.log(result)
res.render('main/labs', { results : result});
}).catch((err)=>{
console.log(err);
res.status(500).render('main/error')
})
});
然而,我总是以同样的错误告终,所以我不确定我是否正确地嵌套了函数调用,或者我忽略了其他东西。
{
_id: 607fc39492b8a4ada9aec28a,
title: 'Lab 01',
description: 'Highlight Table Cells',
filename: '01_lab'
}
]
[2021-04-21T02:51:34.962] [INFO] default FROM=> http://localhost:3000/home, TO=> GET /labs, STATUS=> 500
TypeError: /home/christien/Desktop/web-development/JavaScript_Demos/Labs/resources/main/labs.ejs:15
13| <div class="card" style="width: 15rem;">
14| <div class="card-body">
>> 15| <h5 class="card-title"><%= results[i].title %></h5>
16| <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
17| <p class="card-text"><%=results[i]%></p>
18| <a href="#" class="card-link">Go To Lab</a>
请帮忙!
编辑:
这是我要加载的 ejs 文件
<h3>Labs</h3>
<p>The following are labs that have been completed</p>
<div class="d-flex justify-content-sm-between align-items-center">
<% for (var i =0; i <= results.length; i++ ) { %>
<div class="card" style="width: 15rem;">
<div class="card-body">
<h5 class="card-title"><%= results[i].title %></h5>
<h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
<p class="card-text">Description</p>
<a href="#" class="card-link">Go To Lab</a>
</div>
</div>
<% } %>
</div>
</div>
将查询更改为以下内容
Lab.find().exec().then((results) => {
// handle results
})