【发布时间】:2021-01-01 11:00:33
【问题描述】:
一段时间以来一直在尝试解决这个问题。 我正在将一个包含对象的数组保存到我的数据库中, 当我尝试通过它 map() 来检索对象的属性时,它只是什么都不渲染。
这是 app.js 代码:
app.get("/:plasticcategory/product/:plasticproduct", (req, res) => {
const plasticCategory = _.startCase(_.toLower(req.params.plasticcategory));
const plasticProduct = req.params.plasticproduct;
Product.find({category: plasticCategory, title: plasticProduct},'alt', (err, foundItem) => {
if(err){
console.log(err)
}else {
console.log(foundItem);
res.render('alternatives', {altProduct: foundItem});
}
});
});
当我 console.log(foundItem) 结果是[ { _id: 5f5f9b2a9f999b1e9009072b, alt: [ [Object] ] } ]
这是我的 ejs 代码(试图呈现 alt 的数组对象属性:
<% altProduct.map(alt => {%>
<div class="col-lg-3">
<h1><%=alt.altTitle %></h1>
<img src="<%=alt.altImage%>" alt="alt-image">
<a href="<%=alt.altUrl %>">Get it now!</a>
</div>
<% }) %>
我已添加图片以使其更清晰,谢谢enter image description here
【问题讨论】:
-
尝试
console.log(JSON.stringify(foundItem)),以确认foundItem具有您期望的属性? -
是的,它具有我期望的属性,我使用 console.log(JSON.stringify(foundItem) 并通过我的 mongoDB 集群检查了它
标签: node.js arrays object ejs array.prototype.map