EJS文件添加for循环后出现TypeError,求解决方案
问题解决:EJS循环触发TypeError
错误原因
循环条件写错了:i<=postsItem.length。数组索引从0开始,最后一个元素的索引是postsItem.length - 1。当i等于postsItem.length时,postsItem[i]是undefined,访问它的title属性就会抛出TypeError。
修复后的代码
将循环条件改为小于数组长度即可:
<p><%=homeContent%></p> <% for( i=0 ;i < postsItem.length;i++){ %> <% console.log(postsItem[i].title);%> <% }; %> <%- include('partials/footer') %>
内容的提问来源于stack exchange,提问作者Talha Israr
相关产品推荐
相关产品推荐

