这是我得到的错误:
TypeError: C:\Users\PowerHell\Desktop\fanteziesiculoare\views\store.ejs:81
79| <div class="row gallery-gird">
80| <div class="col-lg-4 col-xl-3 col-sm-6">
>> 81| <% items.music.ForEach(function(item){ %>
82| <div class="shop-item" data-item-id="<%= item.id %>">
83| <a class="gallery-item fresco" href="img/projects/<%= item.imgName %>" data-fresco-group="projects">
84| <img class="shop-item-image" src="img/projects/<%= item.imgName %>" >
Cannot read property 'ForEach' of undefined
at eval (eval at compile (C:\Users\PowerHell\Desktop\fanteziesiculoare\node_modules\ejs\lib\ejs.js:618:12), <anonymous>:11:20)
at returnedFn (C:\Users\PowerHell\Desktop\fanteziesiculoare\node_modules\ejs\lib\ejs.js:653:17)
at tryHandleCache (C:\Users\PowerHell\Desktop\fanteziesiculoare\node_modules\ejs\lib\ejs.js:251:36)
at View.exports.renderFile [as engine] (C:\Users\PowerHell\Desktop\fanteziesiculoare\node_modules\ejs\lib\ejs.js:482:10)
at View.render (C:\Users\PowerHell\Desktop\fanteziesiculoare\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\PowerHell\Desktop\fanteziesiculoare\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\PowerHell\Desktop\fanteziesiculoare\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\PowerHell\Desktop\fanteziesiculoare\node_modules\express\lib\response.js:1008:7)
at C:\Users\PowerHell\Desktop\fanteziesiculoare\server.js:22:11
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:61:3)
这是我的ejs部分:
<% items.music.ForEach(function(item){ %>
<div class="shop-item" data-item-id="<%= item.id %>">
<a class="gallery-item fresco" href="img/projects/<%= item.imgName %>" data-fresco-group="projects">
<img class="shop-item-image" src="img/projects/<%= item.imgName %>" >
<div class="gi-text">
<h4 class="shop-item-title"><%= item.name %> </h4>
<span class="shop-item-price"><%= item.price / 100 %>Ron</span>
</div>
</a>
<button class="btn btn-primary shop-item-button" type="button">Adauga in cos</button> </div>
<% }) %>
这就是我在Node文件中所做的:
const fs = require('fs')
const stripe = require('stripe')(stripeSecretKey)
app.set('view engine', 'ejs')
app.use(express.json())
app.use(express.static('public'))
app.get('/store', function(req, res) {
fs.readFile('items.json', function(error, data) {
if (error) {
res.status(500).end()
} else {
res.render('store.ejs', {
stripePublicKey: stripePublicKey,
items: JSON.parse(data)
})
}
})
})
假设items.music
是一个数组,这只是一个错字。
您需要使用forEach
而不是ForEach
。
<% items.music.forEach(function(item) { %>
如果解决此问题后出现相同的错误,则items.music
属性存在问题。
在服务器中运行console.log(JSON.parse(data).music)
以检查问题。