【发布时间】:2020-04-24 21:04:30
【问题描述】:
我面临错误无法读取属性“forEach of undefined”两天之久,我只是无法解决问题。您的帮助将非常有帮助。下面的代码
bruidstaart.js 页面,我想在其中显示一些数据
<div class="table-wrapper">
<table class="table table-hovered">
<thead class="thead-dark">
<tr>
<th scope="col">Image</th>
<th scope="col">Bruidstaartnaam</th>
<th scope="col">Aantalpersonen</th>
</tr>
</thead>
<tbody>
<% bruidstaarten.forEach((bruidstaart, user) => { %>
<tr>
<td><img src="/assets/bruidstaartenmap/<%= bruidstaart.image %>" class="rounded-circle player-img" alt="" width="200" height="200"></td>
<td><%= bruidstaart.bruidstaartnaam %></td>
<td><%= bruidstaart.aantalpersonen %></td>
</tr>
<% }) %>
</tbody>
</table>
</div>
index.js
exports.bruidstaartenPage = function(req, res, next){
var sql1 = "SELECT * FROM `bruidstaarten` ORDER BY id ASC"; // query database to get all the players
db.query(sql1, function(err, result){
res.render('bruidstaartenPage', {bruidstaarten:result});
});
};
【问题讨论】:
-
forEach 仅适用于数组,所以检查 bruidstaarten 的值是多少?是否为数组
-
你确定结果是数组类型并且里面有有效数据吗?此外,您应该检查 err 是否包含任何正确错误处理的内容。也许它也会告诉你问题。
-
很可能
result未定义,bruidstaarten也未定义 - 通过评估err检查查询是否成功 ... -
对于这个和未来的问题,您可能会发现ericlippert.com/2014/03/05/how-to-debug-small-programs 很有用。正如@eol 所说,
result可能未定义,也许在 index.js 中检查err。 -
您好 Eol、Dcoder 和 Joe Pi,我找到了问题所在。数据库中的 id 错误。创建表格时是我的错。非常感谢您的帮助。现在可以使用了
标签: javascript node.js express ejs