具体来说,我正在尝试(使用通过 Discover Meteor 书构建的非常简单的应用程序)从 Hacker News 中抓取帖子并将它们插入到 Meteor 收藏中,让“新帖子”页面更新,其中包含 20 条左右的新文章。黑客新闻头版。
我的标题上有一个按钮连接到 Meteor.call 事件:
Template.header.events({
'click .hnPull': function() {
Meteor.call('getHnArticles');
}
});
然后(成功地)调用一个名为 getHnArticles 的 Meteor.methods 函数:
getHnArticles: function() {
hn_result = Meteor.http.get('http://news.ycombinator.com');
console.log(hn_result);
$ = cheerio.load(hn_result.content);
var result_set = [];
$('span.comhead').each(function(i, element){ //for ever <span class='comhead'>, do the following
var a = $(this).prev();
var rank = a.parent().parent().text();
var title = a.text();
var url = a.attr('href');
var subtext = a.parent().parent().next().children('.subtext').children();
var points = $(subtext).eq(0).text();
var username = $(subtext).eq(1).text();
var comments = $(subtext).eq(2).text();
//parsed metadata object
var metadata = {
rank: parseInt(rank),
title: title,
url: url,
points: parseInt(points),
username: username,
comments: parseInt(comments)
};
result_set.push(metadata);
});
console.log(result_set);
for (var i = 0; i<20; i++) {
var hn_post = result_set[i];
var postAttributes = {
url: hn_post.url,
title: hn_post.title,
message: 'Scraped automatically from Hacker News'
};
var user = Meteor.user(),
postWithSameLink = Posts.findOne({url: postAttributes.url});
// ensure the user is logged in
if (!user)
throw new Meteor.Error(401, "You need to login to post new stories");
// Make sure the post has a title. it can't be blank
if (!postAttributes.title)
throw new Meteor.Error(422, 'Please fill in a headline');
// Make sure this isn't a duplicate post or repost
if (postAttributes.url && postWithSameLink) {
throw new Meteor.Error(302,
'This link has already been posted',
postWithSameLink._id);
}
// pick out the whitelisted keys
// This keeps a nefarious client from monkeying around with our db
var post = _.extend(_.pick(postAttributes, 'url', 'title', 'message'), {
userId: user._id,
author: user.username,
submitted: new Date().getTime(),
commentsCount: 0,
upvoters: [],
votes: 0
});
Posts.insert(post);
}
},
这样做的最终结果是,来自 Hacker News 的排名最高的帖子被精美地插入。但其他人都没有。
我将名为 result_set 的数组发送到 console.log,它会输出首页:
I202504-11:50:57.551(-5)? [ { rank: 1,
I202504-11:50:57.551(-5)? title: 'Is iOS7 jailbroken yet?',
I202504-11:50:57.551(-5)? url: 'https://isios7jailbrokenyet.com/',
I202504-11:50:57.551(-5)? points: 37,
I202504-11:50:57.552(-5)? username: 'sethbannon',
I202504-11:50:57.552(-5)? comments: 12 },
I202504-11:50:57.552(-5)? { rank: 2,
I202504-11:50:57.552(-5)? title: 'Valve joins the Linux Foundation',
I202504-11:50:57.552(-5)? url: 'http://thenextweb.com/insider/2013/12/04/valve-joins- linux-foundation-prepares-linux-powered-steam-os-steam-machines/',
I202504-11:50:57.553(-5)? points: 276,
I202504-11:50:57.553(-5)? username: 'kwestro',
I202504-11:50:57.554(-5)? comments: 117 },
I202504-11:50:57.554(-5)? { rank: 3,
I202504-11:50:57.555(-5)? title: 'Google Acquires Seven Robot Companies, Wants Big Role in Robotics',
I202504-11:50:57.555(-5)? url: 'http://spectrum.ieee.org/automaton/robotics/industrial-robots/google-acquisition-seven- robotics-companies#.Up9CGN-hd98.hackernews',
I202504-11:50:57.555(-5)? points: 71,
I202504-11:50:57.555(-5)? username: 'eguizzo',
I202504-11:50:57.555(-5)? comments: 29 },
I202504-11:50:57.556(-5)? { rank: 4,
I202504-11:50:57.556(-5)? title: 'Evading Airport Security',
I202504-11:50:57.556(-5)? url: 'https://www.schneier.com/blog/archives/2013/12/evading_airport.html',
I202504-11:50:57.556(-5)? points: 87,
等等。我得到了一个不错的大数组。
知道这里出了什么问题吗?当代码循环通过 Meteor Collections 时,我是否太快地插入记录,还是 Mongo 插入问题?
谢谢!我对 Meteor 很陌生,我喜欢它。但我仍在尝试在 Meteor 中使用异步节点的东西。
编辑:我忘了补充一点,当我查询 MongoDB 实例时,它显示只插入了顶部链接。