【问题标题】:Fetching items from a Meteor collection on the server throws "Can't wait without Fiber"从服务器上的 Meteor 集合中获取项目会抛出“没有 Fiber 无法等待”
【发布时间】:2014-06-03 13:29:24
【问题描述】:

我第一次制作了一个相当简单的流星应用程序,它应该从某个 repo 查询所有 git 问题。从 github api 获取问题列表后,其想法是从这些问题中创建任务集合。但是,每当我尝试查询我得到的当前任务列表时:

.../.meteor/tools/c2a0453c51/lib/node_modules/fibers/future.js:83
W20140418-17:00:43.872(-7)? (STDERR)        throw new Error('Can\'t wait without a fiber');
W20140418-17:00:43.872(-7)? (STDERR)              ^
W20140418-17:00:43.889(-7)? (STDERR) Error: Can't wait without a fiber
W20140418-17:00:43.889(-7)? (STDERR)     at Function.wait    
(.../.meteor/tools/c2a0453c51/lib/node_modules/fibers/future.js:83:9)
W20140418-17:00:43.890(-7)? (STDERR)     at Object.Future.wait    
(.../.meteor/tools/c2a0453c51/lib/node_modules/fibers/future.js:325:10)
W20140418-17:00:43.890(-7)? (STDERR)     at _.extend._nextObject (packages/mongo-    
livedata/mongo_driver.js:805)
W20140418-17:00:43.890(-7)? (STDERR)     at _.extend.forEach (packages/mongo-livedata/mongo_driver.js:836)
W20140418-17:00:43.890(-7)? (STDERR)     at Cursor.(anonymous function) [as forEach] (packages/mongo-  
livedata/mongo_driver.js:695)
W20140418-17:00:43.890(-7)? (STDERR)     at app/server/publish.js:51:33
W20140418-17:00:43.890(-7)? (STDERR)     at Array.forEach (native)
W20140418-17:00:43.891(-7)? (STDERR)     at app/server/publish.js:49:19
W20140418-17:00:43.891(-7)? (STDERR)     at   
...packages/npm/.build/npm/node_modules/github/api/v3.0.0/issues.js:116:17
W20140418-17:00:43.891(-7)? (STDERR)     at IncomingMessage.<anonymous>   
(...packages/npm/.build/npm/node_modules/github/index.js:756:21)

我的第一个想法是,当我应该使用节点光纤时,我在某处使用了回调,但代码似乎相对简单:

var repos = ['my-repo', 'my-repo-1',];
var pollGit = function() {

repos.forEach(function(repo) {
    github.issues.repoIssues({
        user: 'user',
        repo: repo
    }, function(err, stuff) {
        if (err) {
            throw err;
        }
        stuff.forEach(function (issue) {
            var sel = {git_id: issue.id};
            Tasks.find(sel).forEach(function (item) { //ERROR THROWN HERE
                console.log('got', item);
            });
        });
    });
 });
};

Meteor.startup(function() {
    pollGit();
});

每当我在调用 find 后尝试获取实际对象时都会发生此错误。只需调用 find() 即可。究竟是什么导致了错误?

【问题讨论】:

    标签: javascript node.js meteor node-fibers


    【解决方案1】:

    回答我自己的问题以防有人需要答案:

    可以使用 How to insert in Collection within a Fiber?

    代码如下:

    Fiber = Npm.require('fibers');
    var repos = ['my-repo', 'my-repo-1',];
    var pollGit = function() {
    repos.forEach(function(repo) {
        github.issues.repoIssues({
            user: 'user',
            repo: repo
        }, function(err, stuff) {
            if (err) {
                throw err;
            }
            stuff.forEach(function (issue) {
                var sel = {git_id: issue.id};
                Fiber(function() {
                    Tasks.find(sel).forEach(function (item) { //ERROR THROWN HERE
                        console.log('got', item);
                    });
                }).run();
    
            });
        });
     });
    };
    
    Meteor.startup(function() {
        pollGit();
    });
    

    【讨论】:

    【解决方案2】:

    正如@imslavko 指出的那样,等待 Mongo 导致回调的正确方法是使用 Meteor.bindEnvironment(顺便提一下,它有一个使用 GitHub API 的示例)。在你的情况下,

    github.issues.repoIssues({
            user: 'user',
            repo: repo
        }, Meteor.bindEnvironment(function(err, stuff) {
            ...
        }, function () { console.log('Failed to bind environment'); })
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 2015-04-28
      • 2017-05-15
      • 2016-05-26
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      相关资源
      最近更新 更多