从使用 Fiber 到 Meteor.bindEnvironment,我已经尝试了几乎所有我能想到的东西。无论我采用哪种编码方式,都会出现某种光纤错误,或者变量正在被重置。
我的第一次尝试:我尝试将代码包装在纤维中:
Meteor.methods({
addFeed: function() {
try {
var allowedUrls = AllowedUrls.find();
allowedUrls.forEach(function(url) {
request(url.feedUrl)
.pipe(new FeedParser())
.on('error', function(error) {
})// always handle errors
.on('meta', function (meta) {
})// do something
.on('readable', function () {
//===============================================================
// Get the last inserted feed
// iterate through new fetched feeds
// check if the feed mathces last inserted feed
// if it does NOT, save it
//===============================================================
var stream = this, item;
Fiber(function() {
var lastFeedInserted = Feeds.findOne();
var bool = true;
while (item = stream.read() && bool) {
console.log("bool: ", bool);
if (lastFeedInserted) {
if (lastFeedInserted.title !== item.title) {
console.log('lastFeedInserted: ', lastFeedInserted.title);
console.log( "New feed found, item.title: ", item.title );
console.log( "New feed found, last.title: ", lastFeedInserted.title );
Feeds.insert({
title: item.title,
summary: item.description,
url: item.url,
link: item.link,
pubDate: item.pubdate
});
} else {
console.log("bool is being set to false");
bool = false;
break;
}
} else {
Feeds.insert({
title: item.title,
summary: item.description,
url: item.url,
link: item.link,
pubDate: item.pubdate
});
console.log("brand new feed inserted");
}
}
}).run();
});
});
} catch(e) {
console.log("I should go home.", e);
}
},
});
我第二次尝试使用绑定环境:
processFeed = function(item, feedID, lastFeedInserted) {
console.log("last inserted: " + lastFeedInserted + " New feed: " + item.title);
Feeds.insert({
feedID: feedID,
title: item.title
});
};
Meteor.methods({
addFeeds: function() {
try {
var allowedUrls = AllowedUrls.find();
allowedUrls.forEach(function(url) {
var lastFeedInserted = Feeds.findOne({ feedID: url._id });
request(url.feedUrl)
.pipe(new FeedParser())
.on('error', function(error) {
})// always handle errors
.on('meta', function (meta) {
})// do something
.on('readable', function () {
console.log("onreadable called");
//===============================================================
// Get the last inserted feed
// iterate through new fetched feeds
// check if the feed mathces last inserted feed
// if it does NOT, save it
//===============================================================
var stream = this, item;
var bool = true;
while (item = stream.read() && bool) {
console.log("bool: ", bool);
if (lastFeedInserted) {
if (lastFeedInserted.title !== item.title) {
console.log("processFeed");
processFeed(item, url._id, lastFeedInserted, Meteor.bindEnvironment(function(){
console.log("sucess!!!, feed iserted");
}, function(e){
throw e;
}));
} else {
console.log("bool is being set to false");
bool = false;
break;
}
} else {
processFeed(item, url._id, lastFeedInserted, Meteor.bindEnvironment(function(){
console.log("sucess!!!, feed iserted");
}, function(e){
throw e;
}));
Feeds.insert({
feedID: url._id,
title: item.title
});
console.log("brand new feed inserted");
}
}
});
});
} catch(e) {
console.log("I should go home.", e);
}
}
});
第一次尝试有各种各样的问题。变量 bool 总是被重置为 true,即使它被设置为 false,break 语句也不起作用,有时 item.title 是未定义的。
第二次尝试我得到一个错误,因为var lastFeedInserted = Feeds.findOne();
不在光纤中。
我基本上需要找到最后插入的提要,并循环访问items.title
以确保标题不完全相同。换言之,提要尚未保存在数据库中。因此,当我在 while 循环中循环时, 的值不lastFeedInserted
应该改变。如果最后一个lastFeedInserted.title
和item.title
完全相同,那么我需要跳出循环。
我怎样才能使这项工作?