Arangodb - 使用EDGE获得断言错误edge._key等于

时间:2017-12-20 13:21:01

标签: node.js arangodb

我正在与Arangodb和Node.js合作。我正在尝试使用db中的edgecollection。我从npm下载了arangojs并尝试了示例代码。

// ## Assigning the values
const arangojs = require('arangojs');
const aqlQuery = arangojs.aqlQuery;
const now = Date.now();

 //  ## Const variables for connecting to ArangoDB database

 const host = '192.100.00.000'
 const port = '8529'
 const username = 'xyz' 
 const password = 'XYZ'
 const path = '/_db/sgcdm_app/_api/'
 const database = 'sgcdm_app'

// ## Connection to ArangoDB

db = new arangojs.Database({
url: http://${host}:${port},
databaseName: database
});
db.useBasicAuth(username, password);

// ## Working with EDGES

const collection = db.edgeCollection('included_in');
const edge = collection.edge('included_in/595783');
const assert = require('assert');

// the edge exists

assert.equal(edge._key, '595783');
assert.equal(edge._id, 'included_in/595783');
console.log(db);

错误:

assert.js:42
throw new errors.AssertionError({
AssertionError [ERR_ASSERTION]: undefined == '595783'

1 个答案:

答案 0 :(得分:1)

如上所述,edgeCollection.edge() 异步https://github.com/arangodb/arangojs#edgecollectionedge

它返回承诺,而非边缘:

collection.edge('included_in/595783');

Promise {
  <pending>,
  domain:
   Domain {
     domain: null,
     _events: { error: [Function: debugDomainError] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [] } }

您必须await结果或使用then()在结果可用时立即对结果执行某些操作。

collection.edge('included_in/595783')
.then(res => { console.log("Key: " + res._key } ));

Key: 595783

您的断言是assert.equal(edge._key, '595783');,但由于undefined == '595783'错误而失败。 edge实际上是一个Promise对象,它没有_key属性。因此断言错误。

(来自GitHub issue的交叉发布)