How to read this JSON correctly? MSSQL and Node

Asked

Viewed 89 times

3

{
  "recordsets": [
    [
      {
        "IdUsuario": 5152490,
        "strNome": "Roberto ",
        "IdPapel": 1
      },
      {
        "IdUsuario": 5152490,
        "strNome": "Roberto ",
        "IdPapel": 3000001
      }
    ]
  ]

I am trying to return to the customer only his permissions (Idpapel), however, I am not getting.

Follows the code that executes the query and brings as a result the JSON presented:

function verify(query, res, user) {
  sql.connect(dbConfig, (err) => {
    if (err) {
      console.log("Error while connecting database :- " + err);
      res.sendStatus(403);
      sql.close();
    } else {
      var request = new sql.Request();
      request.query(query, (err, result) => {
        if (err) {
          console.log("Error while querying database :- " + err);
          res.sendStatus(403);
          sql.close();
        } else {
          res.send(result);
          sql.close();
        }
      });
    }
  });
}

Any hint?

Thank you!

  • You want to send an array containing each Idpapel this is it?

  • Actually I want to send a JSON to the other side, containing only the papers.

  • Right, so in case you want to send that array of users over there?

  • I would like to submit the name and all the papers of this user. This example json presents the same user several times, however, changing only Idpapel. But if I know how to send only Idpapel, I can send the name. It turns out I can’t get a specific element of the array, from the error.

  • I got what you want. This JSON is what is saved in the result variable there of the correct SQL query?

  • That’s right @Joãopedrohenrique . I’m taking this json from mssql by Node.

  • Okay, I’ll write you an answer then

  • Just one more question, it’s coming back JSON already decoded, correct?

  • It’s returning the JSON the same way I put it in the example. When ordering from the API, it brings this JSON

  • cashed. But this function that makes the call in the database... The result variable is a string with a JSON or it has already decoded the JSON?

  • I can’t say... I even believe that might be why I’m not getting what I want

Show 6 more comments

1 answer

4


To have an array in which each element is one IdPapel, you need to access the array of users returned by the query to the Database. Assuming the answer is stored in the variable result, we can do:

const respostaDecodificada = result
// Aqui estamos pegando o array com os usuários
const arrayUsuarios = respostaDecodificada.recordsets[0]
// Aqui estamos criando um novo array, com o IdPapel de cada usuário
const arrayIdPapel = arrayUsuarios.map(usuario => usuario.IdPapel)

And after the execution of the code, if we give a console.log(arrayIdPapel) we will receive: [ 1, 3000001 ]. From what you said is what you wanted. Then just do res.send(arrayIdPapel).

  • Your arrayUsuarios must be equal to answerDecoded[0], right?

  • In fact, const arrayUsuarios = respostaDecodificada.recordsets[0]. I will correct in response ;)

  • I tried what you proposed and it is returned error in API return :(

  • What appears? The error is in the client or server?

  • In Postman an error message appears and in the server log this message: *59553 connect() failed (111: Connection refused) while Connecting to upstream. If I delete everything and leave the res.send(result), it works normally, however, with the whole message.

  • The problem is not in the answer. Apparently, you are not actually able to connect to your database

  • I’m sure it’s not that, because if I change to res.send(result), I get the result on the client side normally

  • Strange... Suddenly he is already coming the decoded JSON... tries to put const respostaDecodificada = result instead of what you have now...

  • That’s right! Thanks a lot for the help @João Pedro Henrique, it worked! If you can edit the answer so I can be sure

  • I’ll do it now. I’m glad it worked out :D

  • edited response

Show 6 more comments

Browser other questions tagged

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