【发布时间】:2021-08-11 12:18:14
【问题描述】:
我有一个包含各种data 的集合,我希望喜欢在呈现我的ejs 页面时使用这些集合。我似乎只能从 ONE 查询而不是其他查询中呈现内容。
请帮我弄清楚如何从其他查询中呈现内容。
特别注意Transactions from: Unidentified县句中的Unidentified。
现在...让我们看看生成此页面的代码:
function county (req, res) {
transModel.findOne({ transCounty : /Nairobi/i,
})
.limit(5)
.select({ transCounty:1})
.exec( (err, result)=> {
if (err) throw err;
console.log('>> ' +result);
console.log('We in county function!');
return result;
});
};
app.get('/list', async (req,res)=> {
console.log('## ' +county());
transModel.find({transIndustry: 'Pharmacy'}, (err, docs)=> {
if (!err)
{
res.render('list', {data : docs, countyName: county});
}
else
{
// res.status(status).send(body);
}
})
});
在控制台中,上面的代码记录:
## undefined
>> { _id: 609f7ed8fe1fd8b3193b0b77, transCounty: 'Nairobi' }
We in county function!
现在在ejs文件内容下方找到:
<body>
<h1>
Transactions
</h1>
<p> Transactions from: <b> <%= countyName.transCounty %> </b> County: </p>
<table>
<tr>
<th> _id </th>
<th> Transaction Amount </th>
<th> Transaction Industry </th>
</tr>
<% data.forEach(function(entry) {%>
<tr>
<td> <%=entry._id%> </td>
<td> <%=entry.transAmount%> </td>
<td> <%=entry.transIndustry%> </td>
</tr>
<%});%>
</table>
请帮助我了解我哪里出错了,以及如何让<%= countyName.transCounty %> 在我渲染的ejs 文件中显示Nairobi
【问题讨论】:
标签: node.js mongodb express mongoose ejs