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
.– Chance
For some reason, this morning I went to test again and it worked normal, probably I must have forgotten something. : s
– Lone Tonberry
Can you give me an example of how to turn the post into an async call? This disorder continues to abuse me
– Lone Tonberry
Here is an example of a post
async/await
. At first IMAGE 01 we have post('/upload') with two standby functions, the functiongeturl
is dependent on the functionupload
, but for me to ask for each of the functionsawait
i have to create a promise function like on Monday IMAGE 02.– Chance
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.– Chance
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!
– Sorack