[英]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.