0
I’m trying to make a function that can return the values if they’re being passed by the parameters or by the body of a request, the solution I thought was this:
function getReqInfo(req){
let params = []
if (Object.values(req.body) ){ params = Object.values(req.body) } else
if (Object.values(req.params)){ params = Object.values(req.params) }
return params;
}
But even entering one of the scopes the value of the variable params remains the same, I tried that way too:
function getReqInfo(req){
let params = []
if (Object.values(req.body) ){ return Object.values(req.body) } else
if (Object.values(req.params)){ return Object.values(req.params) }
// return params;
}
but it didn’t work either.
I’ve already put this test on:
function getReqInfo(req){
let params = []
if (Object.values(req.body) ){ console.log('teste1') } else
if (Object.values(req.params)){ console.log('teste2') }
return params;
}
And the console.log
s display the message normally, but I am unable to assign the value of Object.values(req.body)
or Object.values(req.params)
for the variable params
or return them
But which error is returned, the request values are not strings?
– LeAndrade
@Leandrade are jsons, I convert the values to vectors and need to assign them to variable params
– arksdf
So man, it’s pretty hard to even try to simulate what might be going on. When you say But even entering one of the scopes the value of the variable stops remains the same, the value would be which, you are able to get the parameters of url and of body right? If you give them a console.log() what is returned?
– LeAndrade
It is as follows: I am doing that scheme Ruthy and falsy to see if the user is sending the request on
req.params
or in thereq.body
. If thereq.params
is void and thereq.body
is completed, the variableparams
should assume the value of the body and vice versa, if I send aconsole.log
within each of the if’s it returns the value smoothly, in either case, which indicates that the if and Else are working, assign the values to theparams
is the problem– arksdf
Consuming the values of Object.values without knowing the keys seems dangerous. Object keys do not have to respect a certain order, so theoretically the Object.values array may have a different order from the elements.
– Sergio
By what I’ve tried Object.values always comes in the same order as the objects were passed, if they respect a default order there is no problem, at least there has been so far...
– arksdf