How to read data from collection in an ArangoDB Foxx app?

368 views Asked by At

I have a collection called 'user' and 1 document in it:

{
  "username": "test1",
  "password": "pw1",
  "id": "1"
}

I created a Foxx app and trying to follow the documentation (using 2.6.0a3). This is my controller:

var Foxx = require('org/arangodb/foxx'),
    usersCollection = applicationContext.collection("user");

var usersRepo = Foxx.Repository.extend({
    getAll: Foxx.createQuery(
        'FOR u IN user RETURN u'
    )
});

var controller = new Foxx.Controller(applicationContext);

controller.get('/', function (req, res) {
    res.json(usersRepo.getAll());
});

I am getting: {"error":"undefined is not a function"} instead of the record list.

What am I missing?

1

There are 1 answers

4
stj On BEST ANSWER

As far as I can see the call to Foxx.Repository.extend() does not instanciate a usable Repository object but a "prototype":

var UsersRepo = Foxx.Repository.extend({
    getAll: Foxx.createQuery(
        'FOR u IN user RETURN u'
    )
});    

What seems to be missing is a concrete object instance:

var usersRepo = new UsersRepo("user");

The instance then can be used to call the predefined functions:

controller.get('/', function (req, res) {
    res.json(usersRepo.getAll());
});

In the above code, I have the prototype in a variable named UsersRepo (upper-case U) and the repository object instance in a variable usersRepo (lower-case u).