javascirpt返回TypeError:未定义的错误

时间:2018-11-08 09:40:31

标签: javascript jquery

我正在执行此代码,尽管它会产生错误。

function getUserPosts(){
  $.get("../php/PostHandler.php", function(data) {
    var posts = JSON.parse(data);
    alert(posts);
    for(var x = lastNumPosts; x <= posts.length; x++){
      var postLine = "<div class='user-post'><div class='user-p-info'><img src='imgs/email-icon2.png'><h2> "+ posts[x].name  +" </h2><br> <small>Posted Tuesday 23/10</small></div><div class='user-p-content'><p>" + posts[x].pContent + "</p></div></div>";
      $('.post-zone').prepend(postLine);
    }
  });
}
  

TypeError:帖子[x]未定义

有解决方案吗?

1 个答案:

答案 0 :(得分:0)

更改此行:

for (var x = lastNumPosts; x < posts.length; x++) {

通知<而不是<=