0

I have a database with documents and I would like to retrieve some data from it providing an array of keys to a Foxx service. It works using a single string but I'm missing something about implementing arrays.

UPDATED

router.get('/keys', function (req, res) {

    const keys = db._query(aql`
        FOR huis IN test
        FILTER huis._key in ${req.queryParams.keys}
        RETURN {
            'adres': huis.adres,
            'postcode': huis.postcode,
            'plaats': huis.plaats
        }
    `);

  res.send(keys);
})
.queryParam('keys', joi.array().required(), 'query to search for')
.response(joi.array()
    .items(
        joi.string().required()
    )
    .required(), 'List of house keys.')
    .summary('List house keys')
    .description('Makes LAT LNG from house keys.');

The joi.array() results in a nice interpretation by Arango on the Services overview page as shown below. But I handle it wrong because it returns a 404.

enter image description here

Thijs
  • 1,423
  • 15
  • 38

1 Answers1

0

If you're passing an array, you will need to use a queryParam or bodyParam and not a pathParam.

I'd recommend change your router path to router.get('/keys', function (req, res) { and then access the value as req.queryParams.keys.

When you send the array via a queryParam you have a few options, either as /keys?keys=1234,5678 or as /keys?keys=1234&keys=5678.

David Thomas
  • 2,264
  • 2
  • 18
  • 20
  • thanks, now I get the message `"query parameter \"keys\" must be an array"` so the array handling is still not ok somehow. joi requieres array, the API page shows an array field, I provide an array and then it says it needs to be an array.. – Thijs Apr 14 '17 at 09:36
  • when I put this in the place where I should add keys with newline seperator it works: `["374875","387781"]` – Thijs Apr 14 '17 at 09:53
  • There you go, there are a number of ways to represent an array with parameters, glad you got it working. – David Thomas Apr 14 '17 at 11:21
  • I appreciate it, it works, but it's not 'the solution' because this is not something that looks ok: `http://localhost:8529/_db/vbo/geo/keys?keys=%5B%22374875%22%5D` so I am still trying to figure out how to handle the array.. – Thijs Apr 14 '17 at 12:23
  • 1
    Have a look at [this stack overflow question](http://stackoverflow.com/questions/11944410/passing-array-in-get-for-a-rest-call), it depends how Foxx recognises it as an array, but that is the strategy typically taken for REST API's and arrays as query params. You can always move them to the body, but that will require you to mark the variable as expected in the body using joi notation. – David Thomas Apr 14 '17 at 12:28