1

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?

rollingBalls
  • 1,808
  • 1
  • 14
  • 25
  • 1
    Did you find that above code somewhere in the docs? If yes, could you post the documentation URL so the example can be fixed? Thanks! – stj Jun 16 '15 at 07:00
  • It's not as above, I was copy pasting one part from the `Details on FoxxRepository` section into the `My first Foxx App` example, but it was my mistake I didn't instantiate the class. Thanks for noticing! – rollingBalls Jun 16 '15 at 10:56

1 Answers1

3

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).

stj
  • 9,037
  • 19
  • 33
  • Thank you! Doing `new UsersRepo` worked. Interestingly, `new Foxx.Repositoy.extend({...})` does not. Would it be too much to ask you to take a look on [this](http://pastebin.com/FqTbv7Jw) snippet as well ? It's my adaptation from the `My first Foxx App` example and I am getting a null error on `.all()`. – rollingBalls Jun 16 '15 at 10:58
  • 1
    The snippet looks ok. However, as `usersCollection` is apparently `null`, then the underlying `user` collection does not seem to exist. Note that your app is referring to the collection via `applicationContext.collection("user")`, which will **not** refer to a collection named `users`, but refer to a collection prefixed with the application mount point and an underscore. For example, if your app is mounted at `/foo`, then the collection name will be `foo_user`. If you're unsure, the name of the collection can be revealed via `require("console").log(applicationContext.collectionName("user"));`. – stj Jun 16 '15 at 11:17
  • 1
    To refer to a collection via an absolute name, your could also use `usersCollection = require("org/arangodb").db._collection("user");` – stj Jun 16 '15 at 11:18
  • Oh, I had no idea about the prefixing. Thank you very much for explaining it! `require("org/arangodb").db._collection("user");` worked like a charm :) – rollingBalls Jun 16 '15 at 13:18