1

I am getting myself up to speed with ArangoDB and Foxx, and so far the experience has been great! I have however one question:

For the Foxx router one can specify queryParams, for instance /graph/nodes?eyes=blue&hair=blond.

What bothers me though is that I am forced (or at least, after several attempts I have such impression) that I have to hard code that I am expecting query params of eyes and hair.

Is this indeed true? And if yes, is there a work around? For me this is a bit strange within the context of nosql and being able to store documents with whatever attributes one wants in a collection.

I'd rather have that I can e.g. fetch the whole part of the URL after ?, do a split("&") on the string and catch whatever was part of the request.

Is there a way to do this within the Foxx framework?

Any help would be greatly appreciated!

Willem van Gerven
  • 1,407
  • 1
  • 17
  • 24

2 Answers2

2

I'm not sure why you think you are forced. The only distinction should be that query params specified as queryParam will be validated (if a schema is provided) and documented.

You should be able to just use req.queryParams to access additional query parameters:

router.get(function (req, res) {
  res.json(req.queryParams);
})
.queryParam('documented', joi.number().optional());

GET /?documented=23 -> {"documented": 23}

GET /?more=42 -> {"more": "42"}

This assumes you are using ArangoDB 3.0 and not running a Foxx services written for 2.8 in legacy compatibility mode (i.e. you're using things called routers, not things called controllers).

Alan Plum
  • 10,814
  • 4
  • 40
  • 57
  • Dear Alan Plum, thank you for your elaborate answer! That is great and I didn't realize it. I managed myself to make it work by using `req.originalUrl`, from which I can split the whole part after `?`. But how you presented it is indeed what I wanted, but somehow missed it can work like this. – Willem van Gerven Aug 05 '16 at 07:45
0

Ok, I managed to find my solution myself, by using var query_params = req.originalUrl.split("?")[1];. Then I get the string that I wanted with raw query parameters. From the manual:

originalUrl: string

Root-relative URL of the request, i.e. path followed by the raw query parameters, if any.

Community
  • 1
  • 1
Willem van Gerven
  • 1,407
  • 1
  • 17
  • 24