混乱を招くことがあり、ここで起こって2つあります。
まず、arangojs(ArangoDBの内部JS APIとは異なり)は、という非同期です。実際のArangoDBサーバーと通信する必要があります。非同期関数は、ドキュメントでは「非同期」としてマークされています。
これらのメソッドにnode.js形式のコールバック(組み込みnode.jsモジュールの非同期関数(例:fs
、http
など)を渡すことができます。代わりに、単にコールバックを省略することができ、メソッドは結果の約束を返します。約束の仕方についてもっと学ぶことができますin Mozilla's JavaScript reference documentation(これはMozilla特有のものではありません。
もう一つは、arangojのコレクションオブジェクトとArangoDBの実際のコレクションの区別です。ドライバは、コレクションが存在するかどうかに関係なく、コレクションのコレクションオブジェクトを作成できます。コレクションが実際に存在しない場合にそれらを使用しようとすると、もちろんエラーが表示されます。 (あなたはバベルを使用するか、今から一年、この答えを読んでいる場合)
var col = db.collection('whatever');
col.create() // create the collection if it doesn't exist
.catch(function() {}) // ignore any errors
.then(function() {
return col.get(); // make sure the collection exists now
})
.then(function() {
return col.save({some: 'data'});
})
.then(function (result) {
// everything went fine
})
.catch(function (e) {
console.error('Something went wrong', e.stack);
});
またはasync /を使用しては待つ:
var col = db.collection('whatever');
try {
await col.create(); // create the collection if it doesn't exist
} catch (e) {} // ignore any errors
try {
await col.get(); // make sure the collection exists now
const result = await col.save({some: 'data'});
// everything went fine
} catch (e) {
console.error('Something went wrong', e.stack);
}
または、Node.jsのスタイルのコールバックを使用して、あなたは、オールドスクールだか、本当に好きなのでピラミッド:
var col = db.collection('whatever');
col.create(function() { // create the collection if it doesn't exist
// ignore any errors
col.get(function (err) { // make sure the collection exists now
if (err) {
console.error('Something went wrong', err.stack);
return;
}
col.save({some: 'data'}, function (err, result) {
if (err) {
console.error('Something went wrong', err.stack);
return;
}
// everything went fine
});
});
});
arangoDBのドキュメントでは動作していますが、ノードのパッケージ「arangojs」はそれに同意しません。存在しないコレクションについては、if節に保持されていると、私のコードは 'true'を示します。 私はarangojsソリューションを望んでいました。現在、コレクションの存在を確認するために、すべてのコレクションのリストでコレクション名を確認しています。しかし、私の方法は非常に原油であり、私はそれ自身が好きではない – Prasanna