从服务器上的Meteor集合中获取项目会导致无法等待没有光纤"

时间:2014-04-19 00:14:22

标签: javascript node.js meteor node-fibers

我第一次制作了一个相当简单的流星应用,它应该从某个回购中查询所有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()工作正常。究竟是什么导致错误?

2 个答案:

答案 0 :(得分:12)

如果有人需要答案,请回答我自己的问题:

使用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();
});

答案 1 :(得分:0)

正如@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'); })
);