Вопрос

I've tried just about everything that I can think of from using Fiber to Meteor.bindEnvironment. No matter which way I code it, I get some kind of fiber error, or variables are being reset.

My First attempt: I tried wrapping the code in a fiber:

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);
            }
        },
});

And my second attempt I tried using the bind environment:

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);
        }
    }
    });

The first attempt had all kinds of problems. The variable bool was always being reset to true, even after it was set to false, the break statement wasn't working, and sometimes the item.title was undefined.

The second attempt I was getting an error because var lastFeedInserted = Feeds.findOne(); was not in a Fiber.

I basically need to find the last inserted feed, and looped through the items.title to make sure that the titles are not the exact same. In other words, the feed has yet to be saved in the database. So while I'm looping through the while loop, the value of lastFeedInserted should not change. If the last lastFeedInserted.title and item.title are the exact same, then I need to break out of the loop.

How can I make this work?

Это было полезно?

Решение

I worked through the issue and came up with the following solution:

https://github.com/digilord/RSS/blob/master/server/methods.js#L176-L230

Sent over a pull request.

:)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top