我第一次制作了一个相当简单的流星应用程序,它应该查询某个回购中的所有git问题。在从githubapi获得问题列表后,我们的想法是根据这些问题创建一个任务集合。然而,每当我试图查询当前任务的列表时,我都会得到:
.../.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()就可以了。究竟是什么原因导致了错误?
回答我自己的问题,以防有人需要答案:
如何在光纤中插入Collection?
代码如下:
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();
});
正如@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'); })
);