Fibers/Meteor.bindEnvironment() の使用に問題があります。コレクションが空で始まる場合、コードを更新してコレクションに挿入しようとしました。これはすべて、起動時にサーバー側で実行されるはずです。
function insertRecords() {
console.log("inserting...");
var client = Knox.createClient({
key: apikey,
secret: secret,
bucket: 'profile-testing'
});
console.log("created client");
client.list({ prefix: 'projects' }, function(err, data) {
if (err) {
console.log("Error in insertRecords");
}
for (var i = 0; i < data.Contents.length; i++) {
console.log(data.Contents[i].Key);
if (data.Contents[i].Key.split('/').pop() == "") {
Projects.insert({ name: data.Contents[i].Key, contents: [] });
} else if (data.Contents[i].Key.split('.').pop() == "jpg") {
Projects.update( { name: data.Contents[i].Key.substr(0,
data.Contents[i].Key.lastIndexOf('.')) },
{ $push: {contents: data.Contents[i].Key}} );
} else {
console.log(data.Contents[i].Key.split('.').pop());
}
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
if (Projects.find().count() === 0) {
boundInsert = Meteor.bindEnvironment(insertRecords, function(err) {
if (err) {
console.log("error binding?");
console.log(err);
}
});
boundInsert();
}
});
}
これを初めて書いたとき、コールバックを Fiber() ブロックにラップする必要があるというエラーが発生しました。その後、IRC での議論で、代わりに Meteor.bindEnvironment() を試すことを推奨する人がいました。それは機能しませんでした (私が見た唯一の出力は でしたinserting...
。つまり、 bindEnvironment() はエラーをスローしませんでしたが、ブロック内のコードも実行しませんでした)。それから私はこれに行きました。私のエラーは次のとおりです。Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
私はノードを初めて使用し、ファイバーの概念を完全には理解していません。私の理解では、スレッドは C/C++/スレッド化されたすべての言語のスレッドに似ていますが、サーバー側のコードに及ぶ影響が何であるか、または挿入しようとしたときにコードがエラーをスローする理由がわかりませんコレクション。誰かが私にこれを説明できますか?
ありがとうございました。