【问题标题】:How can I fix this error: Error: Can't wait without a fiber? [duplicate]如何解决此错误:错误:没有光纤就无法等待? [复制]
【发布时间】:2014-02-28 10:29:16
【问题描述】:

从使用 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.titleitem.title 完全相同,那么我需要跳出循环。

我怎样才能做到这一点?

【问题讨论】:

    标签: javascript node.js meteor node-fibers


    【解决方案1】:

    我解决了这个问题并提出了以下解决方案:

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

    通过拉取请求发送。

    :)

    【讨论】:

      猜你喜欢
      • 2017-11-04
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 2017-08-08
      • 2016-10-25
      • 1970-01-01
      相关资源
      最近更新 更多