Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching items from a Meteor collection on the server throws "Can't wait without Fiber"

I'm making a fairly simple meteor app for the first time that is supposed to query all of the git issues from a certain repo. After it gets a list of issues from the github api, the idea is to create a collection of tasks from these issues. However, whenever I try to query the list of current tasks I get:

.../.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)

My first thought was that I was using a callback somewhere when I was supposed to be using a node-fiber, but the code seems relatively straightforward:

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

This error occurs anytime I try and fetch the actual object after calling find. Just calling find() works fine. What exactly is causing the error?

like image 445
Fenster Avatar asked Apr 19 '14 00:04

Fenster


2 Answers

Answering my own question in case anyone needs the answer:

Got it working with How to insert in Collection within a Fiber?

Code is as follows:

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();
});
like image 77
Fenster Avatar answered Oct 30 '22 02:10

Fenster


As @imslavko pointed out, the correct way to await for Mongo results in a callback is to use Meteor.bindEnvironment (which incidentally has an example using the GitHub API). In your case,

github.issues.repoIssues({
        user: 'user',
        repo: repo
    }, Meteor.bindEnvironment(function(err, stuff) {
        ...
    }, function () { console.log('Failed to bind environment'); })
);
like image 38
Dan Dascalescu Avatar answered Oct 30 '22 03:10

Dan Dascalescu