简体   繁体   English

Tumblr API获取“未捕获的TypeError:无法读取未定义的属性”类型”

[英]Tumblr API getting 'Uncaught TypeError: Cannot read property 'type' of undefined'

I have a series of Tumblr feeds on my site, each retrieving posts from a different tag. 我的网站上有一系列的Tumblr供稿,每个供稿都从不同的标签中检索帖子。

The first feed script works but the 2nd and 3rd throw the error: 第一个提要脚本有效,但是第二个和第三个引发错误:

Uncaught TypeError: Cannot read property 'type' of undefined

Each script is exactly the same accept for the tag retrieve and the element the data it is appended to. 每个脚本对于标签检索及其附加到数据的元素完全相同。

I am having trouble figuring this out. 我很难弄清楚这一点。 Does anyone know what I've done wrong? 有人知道我做错了吗?

$.ajax({
    url: "http://api.tumblr.com/v2/blog/myblog.tumblr.com/posts?api_key=mykey&tag=news",
    dataType: 'jsonp',
    success: function(results){

    var i = 0;

     while (i < 10) {

       var type = results.response.posts[i].type;
       var date = results.response.posts[i].date;
       var link = results.response.posts[i].post_url;

       if (type == "text") {
         var title = results.response.posts[i].title;
         var content = results.response.posts[i].body;
         $("#tumnews #newscara").append("<div class='tumpost'><a href='" + link + "'><h2>" + title + "</h2>" + content + "</a></div>");
       }
       else if (type == "photo") {
         var photourl = results.response.posts[i].photos[0].alt_sizes[0].url;
         $("#tumnews #newscara").append("<li><div class='tumpost'><a href='" + link + "'><img src='" + photourl + "' alt='" + title + "'/></a></div></li>");
       }

      i++;
     }//END WHILE


  $("#newscara").flexisel({
    visibleItems: 5,
    animationSpeed: 200,
    autoPlay: true,
    autoPlaySpeed: 3000,
    pauseOnHover: true,
    clone:false,
  });

    }//END RESULTS FUNCTION
});

I found the error was becuase there were fewer posts in the other two tags I was retrieving, and because I specified while (i < 10) it kept looping through and if there were less then 10 posts the error would appear. 我发现错误是因为我正在检索的其他两个标签中的帖子减少了,并且因为我指定了while (i < 10)它不断循环,如果少于10个,则会出现错误。

I ammended it by replacing the while statement with 我通过将while语句替换为

while (i < results.response.posts.length)

so it would only loop through the amount of posts that actually existed. 因此它只会遍历实际存在的帖子数量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我得到 Uncaught (in promise) TypeError: Cannot read property &#39;type&#39; of undefined? - Why am I getting Uncaught (in promise) TypeError: Cannot read property 'type' of undefined? 获取&#39;未捕获的TypeError:无法读取属性&#39;地理编码&#39;未定义&#39;错误 - Getting 'Uncaught TypeError: Cannot read property 'geocode' of undefined ' error 为什么会出现 Uncaught TypeError:无法读取未定义的属性“dataSource” - Why getting Uncaught TypeError: Cannot read property 'dataSource' of undefined 获取错误未捕获的TypeError:无法读取未定义的属性“ toLowerCase” - Getting error Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 使用地图框和传单获取“未捕获的类型错误:无法读取未定义的属性 &#39;lat&#39;” - Getting “Uncaught TypeError: Cannot read property 'lat' of undefined” with mapbox and leaflet 偶尔收到“未捕获的TypeError:无法读取未定义的属性&#39;push&#39;” - Getting “Uncaught TypeError: Cannot read property 'push' of undefined” sporadically 在JavaScript上获取“未捕获的TypeError:无法读取未定义的属性&#39;top&#39;” - Getting “Uncaught TypeError: Cannot read property 'top' of undefined” on javascript 我收到“未捕获的类型错误:无法读取未定义的属性‘注释’”错误 - Im getting a "Uncaught TypeError: Cannot read property 'note' of undefined" error 获取'未捕获的类型错误:无法读取未定义的属性'帮助者'' - Getting 'Uncaught TypeError: Cannot read property 'helpers' of undefined' 我在 axios 补丁 api 中收到“Uncaught (in promise) TypeError: Cannot read property 'data' of undefined”错误 - I'm getting “Uncaught (in promise) TypeError: Cannot read property 'data' of undefined” error in axios patch api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM