fgalan
fgalan

Reputation: 12294

Edges collection undefined until _collections() operation is used

I'm using ArangoDB 3.4.2 and I have a weird problem that I'm not able to explain...

I create a graph (myGraph) in the following in arangosh:

var graph_module = require('@arangodb/general-graph');

var myGraph = graph_module._create('mygraph');
myGraph._addVertexCollection('vertexes');
var edges = graph_module._relation('edges', ['vertexes'], ['vertexes']);
myGraph._extendEdgeDefinitions(edges);

Being vertexes and edges the collections for vertexes and edges, respectively.

Now, I create two vertexes:

db.vertexes.save({"name": "A", "_key": "A"});
db.vertexes.save({"name": "B", "_key": "B"});

So far so good. But now I try to create the edge between both and I get a fail:

127.0.0.1:8529@myDB> db.edges.save("vertexes/A", "vertexes/B", {"name": "A-to-B"});
JavaScript exception: TypeError: Cannot read property 'save' of undefined
!db.edges.save("vertexes/A", "vertexes/B", {"name": "A-to-B"});
!        ^
stacktrace: TypeError: Cannot read property 'save' of undefined
    at <shell command>:1:9

It seems that db.edges is undefined:

127.0.0.1:8529@MyDB> console.log(db.edges)
2019-01-26T19:01:52Z [98311] INFO undefined

But now, if I run db._collections() it seems that db.edges gets defined (weird!)

127.0.0.1:8529@MyDB> db._collections()
...
127.0.0.1:8529@MyDB> console.log(db.edges)
2019-01-26T19:02:58Z [98311] INFO [ArangoCollection 16807, "edges" (type edge, status loaded)]

and in this moment, the db.edges.save(...) operation works:

127.0.0.1:8529@MyDB> db.edges.save("vertexes/A", "vertexes/B", {"name": "A-to-B"});
{ 
  "_id" : "edges/16899", 
  "_key" : "16899", 
  "_rev" : "_YGsKKq2--_" 
}

Why db.edges is undefined at the first save()? Why a show colletions operation (which I understand is read-only) is getting it defined? Maybe I'm doing something wrong?

Upvotes: 0

Views: 48

Answers (1)

Maximilian Kernbach
Maximilian Kernbach

Reputation: 571

When executing db.edges.save() an internal cache is accessed. If this cache is clear, executing db.edges.save() works to save an edge. Since db._collections() resets this cache, it is possible to run the command afterwards. However if this cache is not clear, an error is thrown as you observed.

The correct and safe way is to access the collection via db._collection("collection-name"). Therefore you can use the following command to save an edge in the edges collection:

db._collection("edges").save("vertexes/A", "vertexes/B", {"name": "A-to-B"});

Upvotes: 1

Related Questions