Problem to Insert Data in BD

Asked

Viewed 31 times

0

I created a form that inserted information in Mysql using Nodejs, but it is not inserting anything and not any error in the console and none in the terminal, follows below the code

admin.js

 module.exports = function(application){
    application.get('/formulario_inclusao_noticia', function(req,res){
        res.render('admin/form_add_noticia');
    });
 
    application.post('/noticias/salvar', function(req,res){
        var noticia = req.body;
 
        var connection = application.config.dbConnection();
        var noticiasModel = application.app.models.noticiasModel(connection); //sugestão informada nos comments
 
        noticiasModel.salvarNoticia(noticia, connection, function(error, result){
            res.redirect('/noticias');
        });
    });
}
noticiasModel.js

module.exports = function(){
 
    this.getNoticias = function(connection, callback){
        connection.query('SELECT * FROM noticias', callback);
    }
 
    this.getNoticia = function(connection, callback){
        connection.query('SELECT * FROM noticias WHERE id_noticia = 2', callback);
    }
 
    this.salvarNoticia = function(noticia, connection, callback){
        connection.query('INSERT INTO noticias SET ?', noticia, callback);
    }
 
    return this;
 
}
//form_add_noticia.ejs

<!DOCTYPE html>
    <html lang="pt-br">
        <head>
            <meta charset="utf-8"/>
            <title>Formulário de cadastro de Notícia</title>
        </head>
    <body>
 
        <h1>Adicionar notícia</h1>
        <br />
 
        <form action="/noticias/salvar" method="post">
            <label>Título</label>
            <input type="text" id="titulo" name="titulo" placeholder="Título da Notícia" />
            <br />
            <label>Notícia</label>
            <textarea id="noticia" name="noticia" rows="5" cols="30"></textarea>
            <br />
            <input type="submit" value="Enviar" />
        </form>
 
    </body>
</html>

1 answer

0


The problem is in the name of the News textarea

//Antes
<br />
  <label>Notícia</label>
  <textarea id="noticia" name="noticia" rows="5" cols="30"></textarea>
 <br />
 
 //Depois
 
 <br />
  <label>Notícia</label>
  <textarea id="noticia" name="noticias" rows="5" cols="30"></textarea>
 <br />

Browser other questions tagged

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