我使用stockfish npm包获得collaborative chess game:
它以非常灵活的方式编写,似乎我必须像这样使用它:
Evaluator = (function() {
var Promise = Meteor.npmRequire('promise');
var ev = Meteor.npmRequire('stockfish')();
var promise = new Promise(function (resolve, reject) {
var line;
ev.onmessage = function(e) {
if (e && typeof e === "object") {
line = e.data;
} else {
line = e;
}
if (line.indexOf('Total Evaluation') > -1) {
var score = parseFloat(line.split('Total Evaluation: ')[1].split('(')[0])
resolve(score);
}
};
});
return function(movesStr) {
ev.postMessage('position startpos moves ' + movesStr);
ev.postMessage("eval");
return promise;
}
})();
并称之为:
Meteor.methods({
evaluate: evaluate
});
function evaluate(movesStr) {
return Evaluator(movesStr);
}
所以从我的模型来看,电话看起来像是这样:
Meteor.call('evaluate', movesStr)
.then(function(gameScore) {
log('gameScore', gameScore);
Games.update(
{ _id: gameId },
{ $set: { score: gameScore } },
function(err, docs) {
log('cacheGameScore', 'err', err)
}
);
});
此代码"工作",我可以在控制台上看到准确的结果。
然而,数据库调用Games.update
触发此错误:
[Error: Meteor code must always run within a Fiber.
Try wrapping callbacks that you pass to non-Meteor
libraries with Meteor.bindEnvironment.]
我尝试在很多地方添加Meteor.wrapAsync和Meteor.bindEnvironment,我总是收到这个错误信息。
在这里尝试的任何想法?