0
在使用tumblr API建設項目,當有分配給它的值的數組對象返回的空 - 的Javascript
var latest = [];
client.posts('XXXXXX', function (err, blog) {
for (var i = 0; i <
blog.posts.length; i++) {
var post = blog.posts[i];
// this condition is used to identify if any blog post is dated no later than 7 days.
if ((new Date().valueOf() - Date.parse(post.date)) /(1000*60*60*24) <= 7){ latest.push(post);
// console.log(post);
}
};
});
console.log(latest)
最新的是一個變量存儲任何博客文章對象滿足條件。當取消註釋的console.log(後),命令行打印張貼對象是這樣的:
{ blog_name: '......',
id: ......,
post_url: '......',
slug: '......',
type: 'text',
date: '2015-07-20 16:42:30 GMT',
timestamp: ....,
state: 'published',
format: 'html',
reblog_key: '.....',
tags: [],
short_url: '.......',
recommended_source: null,
followed: false,
highlighted: [],
liked: false,
note_count: 0,
title: '.....',
body:.......}
然而,對於執行console.log(最新),命令行打印[]。爲什麼最近在這裏返回一個空數組?
它看起來像'client.posts()'是一個異步方法......在這種情況下,你正在嘗試將無法正常工作......你需要有回調方法中的'console.log()'。 –