Update method does not update in the database

Asked

Viewed 57 times

0

With a problem when editing, when I click on the button it looks for the information of the fields in the console.log, already has the ID on the route also the problem is that it does not update the information in the database, follows code:

exports.edit = function (req, res){
    var editaRegiao = req.body;
    var municipiosarray = [];

    for(var i = 0; i < editaRegiao.municipio.length; i++){
      municipiosarray.push(editaRegiao.municipio[i].id);
    }
    municipiosarray.toString()

    console.log(req.body);
      client.query('update cnes.regiao set descricao = ' + editaRegiao.descricao + ', municipio = ' + editaRegiao.municipio + 'where codigoregiao = ' + req.params.id, (err, result) => {
        if (err) return res.json(err);
        console.log(err);
        console.log(result);
        return res.json(result.rows);
      })
};

1 answer

0


I believe the problem is in the SQL mount, because the field descrição seems to me to be a string, and the way it is mounted you do not use quotes (Valid also for other fields of type string.

Try it like this:

exports.edit = function (req, res){
    var editaRegiao = req.body;
    var municipiosarray = [];

    for(var i = 0; i < editaRegiao.municipio.length; i++){
      municipiosarray.push(editaRegiao.municipio[i].id);
    }
    municipiosarray.toString()

    console.log(req.body);
      client.query('update cnes.regiao set descricao = "' + editaRegiao.descricao + '", municipio = "' + editaRegiao.municipio + '" where codigoregiao = ' + req.params.id, (err, result) => {
        if (err) return res.json(err);
        console.log(err);
        console.log(result);
        return res.json(result.rows);
      })
};

And another, it lacked a space between its municipality and its clause where.

I hope I’ve helped.

  • solved, it was just that, thank you!!

  • Do not forget to give a + in response and mark as the best for kindness.

Browser other questions tagged

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