javascript - 如何修复此错误 : Error: Can't wait without a fiber?

标签 javascript node.js meteor node-fibers

我已经尝试了几乎所有我能想到的方法,从使用 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);
            }
        },
});

我的第二次尝试是使用绑定(bind)环境:

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完全相同,那么我需要跳出循环。

我怎样才能做到这一点?

最佳答案

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

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

通过拉取请求发送。

:)

关于javascript - 如何修复此错误 : Error: Can't wait without a fiber?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21552735/

相关文章:

javascript - $.isNumeric 与 isNaN

javascript - 修改对象数组以按键和值分组为嵌套的父/子对象数组

node.js - Angular-universal - 生产问题

node.js - 如何同步 Node js 中加密模块的 crypto.randomBytes() 函数?

javascript - Electron js在main.js之外使用desktopCapture

javascript - Meteor:在 Meteor 应用程序中包含 Admob Interstitial 广告

Meteor - 丢失对象属性的简单模式

javascript - 将焦点设置为仅在父级获得焦点时才显示的元素

javascript - 谷歌分析 : Tracking Not Installed though real time data is showing

javascript - 使用 MeteorJS 和 Iron Routing 返回到之前的路径