-6ren">
gpt4 book ai didi

javascript - ejs 的问题(data.forEach 不是函数)!

转载 作者:行者123 更新时间:2023-12-02 17:22:26 27 4
gpt4 key购买 nike

你好,我正在尝试在 ejs 文件中运行一个 javascript 函数,这就是我的代码:

<div class='row'>

<% data.forEach( function( items ) { %>

<div class='col-md-4 col-sm-6'>
<div class="thumbnail">
<img src="<%= items.img %>" width="350" height="130"></img>

<div class="caption">
<h4><%= items.partname %></h4>
</div>
</div>



</div>

<% }); %>

</div>

当我尝试在我的 ejs 文件中运行它时,我收到此错误作为返回值“ 36| 37|

38| <% data.forEach(function(items){ %> 39|
40| 41|

data.forEach 不是函数 在评估(评估在

有人知道怎么解决吗?

这是我上面代码的后端:

app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine","ejs");

var chairSchema=new mongoose.Schema({
partname:String,
img:String,
price:Number
});

var data =mongoose.model("data",chairSchema);

data.create(
{
partname:"short cylinder",
img:"http://www.needforseatusa.com/assets/images/products/replacement%20parts/short_cylinder_thumbnail.jpg",
price:14.90
},
{
partname:"regular cylinder",
img:"http://www.needforseatusa.com/assets/images/products/replacement%20parts/cylinder_thumbnail.jpg",
price:14.90
},{
partname:"back pillow",
img:"http://www.needforseatusa.com/assets/images/products/replacement%20parts/lumbar_pillow_thumbnail.jpg",
price:29.90
},{
partname:"head pillow",
img:"http://www.needforseatusa.com/assets/images/products/replacement%20parts/head_pillow_thumbnail.jpg",
price:29.90
},{
partname:"wheel base chrome",
img:"http://www.needforseatusa.com/assets/images/products/accessories/hd-base-black_thumbnail.jpg",
price:79.99
},{
partname:"wheel base black",
img:"http://www.needforseatusa.com/assets/images/products/accessories/hd_base_)1_thumbnail.jpg",
price:79.99
},function(err,chair){
if (err){
console.log(err);
}
else{
console.log("newly created chair");
console.log(data);
}

}


);
app.get("/",function(req,res){
res.render('landing');
});


app.get("/campground",function(req,res){
data.find({},function(err,data){
if(err){
console.log(err);
}

});

res.render("campground", {data:data});
});

最佳答案

这是一个使用 foreachejs 的示例,我希望您检查发送回 ejs 的数据,如果它是数组。否则请确保它是,因为 foreach 是一个数组方法。

var data = {    
title: 'Cleaning Supplies',
supplies: ['mop', 'broom', 'duster']
};

ejs

<ul>
<% data.supplies.forEach(function(value) { %>
<li><%= value %></li>
<% }) %>
</ul>

// mop
// broom
// duster

关于javascript - ejs 的问题(data.forEach 不是函数)!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41708284/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com