I wrote the following router in Foxx Microservices:
router.post('/PersonInformation', function (req, res) {
const keys = db._query(aql`
FOR p IN Personal
FILTER (p.stateId IN ${req.queryParams.statePar} OR IS_NULL(${req.queryParams.statePar}))
AND p.empDate >= TO_NUMBER(${req.queryParams.fromDatePar}) AND p.empDate <= TO_NUMBER(${req.queryParams.toDatePar})
RETURN p
`);
res.send(keys);
})
.queryParam('statePar', joi.array().default(null), 'State Parameter')
.queryParam ('fromDatePar', joi.string().required(), 'FromDate Parameter')
.queryParam('toDatePar', joi.string().required(), 'ToDate Parameter')
.response(joi.array().items(
joi.string().required()
).required(), 'List of entry keys.')
.summary('List entry keys')
.description('Assembles a list of keys of entries in the collection.');
How i can convert queryParam to body parameter. I used .body instead of .queryParam, but it does not answer. I also wrote the table as follows, but it still does not work:
router.post('/PersonInformation', function (req, res) {
const distributorIdPar = req.body;
const cityPar = req.body;
const productIdPar = req.body;
const fromDatePar = req.body;
const toDatePar = req.body;
const keys = db._query(aql`
const keys = db._query(aql`
FOR p IN Personal
FILTER (p.stateId IN ${req.body.statePar} OR IS_NULL(${req.body.statePar}))
AND p.empDate >= TO_NUMBER(${req.body.fromDatePar}) AND p.empDate <= TO_NUMBER(${req.body.toDatePar})
RETURN p
`);
res.send(keys);
})
.response(joi.array().items(
joi.string().required()
).required(), 'List of entry keys.')
.summary('List entry keys')
.description('Assembles a list of keys of entries in the collection.');
You have a duplicate line
const keys = db._query(aql`
that breaks the syntax. Furthermore, you are not setting.body(joi.object())
or.body(['application/json'])
, which leads toreq.body
being a Buffer:You also assign
req.body
to various variables but don't actually use them.I would assign the body parameters to local variables using destructuring for the guaranteed values, and use a regular assignment for
statePar
so that it can default tonull
if it is omitted from the body. Furthermore, I would enforce numeric values forfromDatePar
andtoDatePar
(note that this requiresstrict()
in joi or it will happily accept number strings) and removeTO_NUMBER()
from the query. Below code accepts either an array of strings forstatePar
or expects the attribute to not be present. It disallows"statePar":null
,"statePar":[]
, and"statePar":[null]
, but you could change that if desired.