How to access json Response fields in nodejs?

Asked

Viewed 103 times

1

I’m studying a little bit about nodejs and am facing difficulties to capture and access json Sponse, For example: My Getbyid

exports.getProduto = function(req, res) {
    const produtoId = req.params.id
    const queryString = "SELECT * FROM produto WHERE idproduto = ?;"
       pool.query(queryString, [produtoId], (err, rows, fields) => {
        if (err) {
            console.log('Erro: ' + err)
            res.sendStatus(500)
            res.end()
            return
        }
        console.log('SUCESSO!')
        res.json(rows)
    }) 
   }

When I give the get I have this answer. However I would like to access the json data(fields) and I don’t know how I do it.

[
{
    "idproduto": 1,
    "nome": "LANCHE",
    "preco": 20,
    "imagem": null
}
]
  • How to store the fields individually?

  • Try to rephrase the question, maybe something in the sense of json return from a database answer, your question is misspelled.

  • I just changed.

1 answer

2


Query(example):

[
 {
    "idproduto": 1,
    "nome": "LANCHE1",
    "preco": 20,
    "imagem": null
 },
 {
    "idproduto": 2,
    "nome": "LANCHE2",
    "preco": 20,
    "imagem": null
 }
]

    rows[0].nome //output: LANCHE1
    rows[1].nome // output: LANCHE2

Rows -> query result
[0] -> line I want to see the data
name -> column name I want the data

Browser other questions tagged

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