我正在尝试在EJS中实现无限滚动,并尝试首先呈现数据库项,但仅将其限制为1。因此,我在ejs中创建了一个测试页,并能够以明文呈现数据库中的第一项。
express / mongoose服务器端:
app.get("/test",(req,res)=>{
database.find({}, (err, found) => {
if (!err){
res.render("test",{
db :found,
logStatus :true,
});
} else {
console.log(err);
}
}).limit(1);
});
EJS:
<boilerplate code for header and navbar>
<p><%=db%></p>
<boilerplate code for footer>
此部分有效,因此我接下来尝试分别加载数据库内容:
express / mongoose服务器端:
app.get("/test",(req,res)=>{
database.find({}, (err, found) => {
if (!err){
res.render("test",{
db :found,
link :found.link,
name :found.name,
price :found.price,
logStatus :true,
});
} else {
console.log(err);
}
}).limit(1);
});
[EJS侧
<boilerplate code for header and navbar>
<p><%=link%></p>
<boilerplate code for footer>
这没有显示任何内容,所以我完全不知道为什么。
find()
方法返回一个文档数组,因此found
是一个数组,您无法在其上调用.link
。您可以将其更改为found[0].link
或改用findOne()
方法。