Registry is not being sent to the database

0

When I use 'insert into news set?', to register at the bank, it is simply not registered

Model:

module.exports = function() {
    ...

    this.salvarNoticia = function(noticia, connection, callback) {

        connection.query('insert into noticias set ?', noticia, callback)
    }

    return this;
}

admin.js:

module.exports = function(application) {
    ...

    application.post('/noticias/salvar', (req, res) => {
        var noticia = req.body; 

        var connection = application.config.dbConnection();

        var noticiasModel = application.app.models.noticiasModel;

        noticiasModel.salvarNoticia(noticia, connection, function(error, result) {
            res.redirect('/noticias');
        })

    })  

}

I'm using the consign, the data coming from the form are all ok, so much so that if I try to register in the bank this way:

connection.query('insert into noticias(titulo, noticia) values ('${dadoRetornadoDoForm.titulo}', '${dadoRetornadoDoForm.noticia}')', dadoRetornadoDoForm, callback)

The registration is usually done, where is the error?

Note: the version of my MySQLServer is 8.0

    
asked by anonymous 13.08.2018 / 01:16

1 answer

0

I suggest you make the following changes to your code so that at least one error is returned if the operation is not successful. First change your model to the following:

const { promisify } = require('util');

const salvarNoticia = async (noticia, conexao) => {
  const query = promisify(conexao.query);

  return query('INSERT INTO noticias (titulo, noticia) values ('${noticia.titulo}', '${noticia.noticia}')');
}

module.exports = {
  salvarNoticia,
};

Your controller will look like this:

// Faça aqui o require da model que será referenciada depois como "noticiasModel" e da sua conexão que será referenciada com o nome "conexao"

const create = async (req, res) => {
  try {
    const { body: noticia } = req;
    await noticiasModel.salvarNoticia(noticia, conexao);
    res.redirect('/noticias');
  } catch(e) {
    console.error(e);
    res.status(500).send('Ocorreu um erro interno.');
  }
}

module.exports = {
  create
};

And the route call will be as follows:

application.post('/noticias/salvar', noticiasController.create);

In this way, if an error occurs, it will appear completely in console of Node.js and the request will fail as expected.

    
22.08.2018 / 03:39