Nodejs - Change the value of a vector in an if Else

Asked

Viewed 81 times

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.logs 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 are jsons, I convert the values to vectors and need to assign them to variable params

  • 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?

  • 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 the req.body. If the req.params is void and the req.body is completed, the variable params should assume the value of the body and vice versa, if I send a console.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 the params is the problem

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

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

Show 1 more comment

2 answers

1


in javascript we have more than just true and false values, we have Truthy and falsy, when you pass an empty json to the Object.values() function, it returns an array, if json is empty it will return an empty vector, and an empty vector is truty!!

array vazio js

So every time you enter this code it will enter that first if, even without anything in json (if Undefined in the case the Object.values function will trigger an exception) then the correct check to do when comparing if there are values in json is:

if (Object.values(req.body).length > 0) { /* código */ }

this way will buy the size of the vector, if there is any value it will enter in the if.

  • After I read your answer calmly I realized that it solves my problem, I misunderstood you, sorry...

0

This is happening because you are using Else if and not only an Else, as in the two checks (if) it returns false, the params variable returns nothing, because it really has nothing to return.

I suggest you remove ifs and with console log. even and check what you are getting return from the function and use the if else to do the checks, do this, if not, do that, it will only have two ways. NOTE: You have to make sure that it returns only that two ways even if you don’t have an error.

  • So, there are times when I will want both conditions to be false and the vector returns empty even. I have already put a console log. in both, and it enters the scope normally, the problem is that I can’t assign the value of Object.values(req.body) or Object.values(req.params) for the variable params

  • Can’t because? It always returns empty? params is an array, trying to give a params.push(Object.values(req.body))

  • yes he always returns as a [] independent if any of the conditions is true. I tried to use push as you said, it did not work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.