Registration is not being sent to the database

Asked

Viewed 68 times

0

When I use 'Insert into noticias set ? ', to register in 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 is all ok, so 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 done normally, where the error is?

Note: the version of my Mysqlserver is 8.0

  • Do not return error? I think it should be the question of asynchrony. Turns the post into an async call and asks for Connection, news, and model news.. async/await.

  • For some reason, this morning I went to test again and it worked normal, probably I must have forgotten something. : s

  • Can you give me an example of how to turn the post into an async call? This disorder continues to abuse me

  • Here is an example of a post async/await. At first IMAGE 01 we have post('/upload') with two standby functions, the function geturl is dependent on the function upload, but for me to ask for each of the functions await i have to create a promise function like on Monday IMAGE 02.

  • If you ask me what the advantage of async/await Instead of callback, I would say that the code gets much cleaner and organized, it gets easier to understand. In the case of your code we would have to see if your model can be a promise, if it is sequelize or Mongoose is very simple to implement the promise, the salvage news is already a promise so await would be enough, since all bank calls are promises.

  • Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!

Show 1 more comment

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 performed successfully. 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);

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

Browser other questions tagged

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