私は、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);
}
},
});
そして、バインド環境を使用して試した2回目の試み:
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();
2 回目の試行では、ファイバーに含まれていなかったため、エラーが発生しました。
私は基本的に、最後に挿入されたフィードを見つける必要がありitems.title
、タイトルがまったく同じではないことを確認するために をループしました。つまり、フィードはまだデータベースに保存されていません。そのため、while ループをループしている間、 の値はlastFeedInserted
変更されません。最後のlastFeedInserted.title
とitem.title
がまったく同じである場合は、ループから抜け出す必要があります。
どうすればこれを機能させることができますか?