我已经尝试过使用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();
不在光纤中。
我基本上需要找到最后插入的Feed,并循环浏览items.title
以确保标题不完全相同。换句话说,Feed尚未保存在数据库中。因此,当我循环遍历while循环时,lastFeedInserted
的值不应该更改。如果最后一个lastFeedInserted.title
和item.title
完全相同,那么我需要突破循环。
我该如何做到这一点?
答案 0 :(得分:0)
我解决了这个问题并提出了以下解决方案:
https://github.com/digilord/RSS/blob/master/server/methods.js#L176-L230
通过拉取请求发送。
:)