Error saving an input text

0

By saving an input-text in a Mysql database, it will not let me. When I tap on submit I get the following error

  

TypeError: Can not read property 'inputemail' of undefined       at routes.post (C: \ Users \ Piter \ Desktop \ Goven \ server \ routes \ routes.js: 11: 31)       at Layer.handle [as handle_request] (C: \ Users \ Piter \ Desktop \ Goven \ node_modules \ express \ lib \ router \ layer.js: 95: 5)       at next (C: \ Users \ Piter \ Desktop \ Goven \ node_modules \ express \ lib \ router \ route.js: 137: 13)       at Route.dispatch (C: \ Users \ Piter \ Desktop \ Goven \ node_modules \ express \ lib \ router \ route.js: 112: 3)       at Layer.handle [as handle_request] (C: \ Users \ Piter \ Desktop \ Goven \ node_modules \ express \ lib \ router \ layer.js: 95: 5)       at C: \ Users \ Piter \ Desktop \ Goven \ node_modules \ express \ lib \ router \ index.js: 281: 22       at Function.process_params (C: \ Users \ Piter \ Desktop \ Goven \ node_modules \ express \ lib \ router \ index.js: 335: 12)       at next (C: \ Users \ Piter \ Desktop \ Goven \ node_modules \ express \ lib \ router \ index.js: 275: 10)       at Function.handle (C: \ Users \ Piter \ Desktop \ Goven \ node_modules \ express \ lib \ router \ index.js: 174: 3)       at router (C: \ Users \ Piter \ Desktop \ Goven \ node_modules \ express \ lib \ router \ index.js: 47: 12)

This is my HTML code:

<div id="screen">
            <form action="/contact" method="post">
                <h5>Email</h5>
                <input type="text" id="inputemail" name="inputemail" >
                <h5>Comentario</h5>
                <textarea id="areacoment" name="areacoment"></textarea>
                <button type="submit" id="btnsubmit">Enviar</button>
            </form>
       </div>

This my routes:

const express = require("express");
const routes = express.Router();
const pool= require("../mysql/mysql");
const mysql=require("../mysql/mysql");


routes.post("/contact",(req,res)=>{
    var inputemail = req.body.inputemail;
    pool.query("INSERT INTO contact (email, info) VALUES (?, ?)", [inputemail, inputcoment],
        function (error, results, fields) {
            if (error) { throw error }
            if (results) { console.log(results); }
        }
    );
    res.render("index.html");
});

module.exports=routes;

This is my index.js:

const express = require("express");
const app = express();
const morgan = require("morgan");
const path = require("path");
const routes = express.Router();
const mysql = require("mysql");
const connection = require("express-myconnection");

//SETTINGS
app.set("port", process.env.PORT || 8080);
//ROUTES
app.use(require("./routes/routes"));
//STATIC FILES
app.use(express.static(path.join(__dirname,"public")));
//MIDDLEWARES
app.use(express.urlencoded({extended:false}));
//MYSQL
app.use(connection(mysql,{
host:"localhost",
user: "pedri",
password:"cocoypaco2",
database:"vegi",
port:3306
},"single"));

//LISTEN SERVER
app.listen(app.get("port"),()=>{
console.log("Server on port ", app.get("port"));
});
    
asked by Pedro Danderfer 24.12.2018 в 21:28
source

1 answer

0

You need to put a middleware to manipulate the post data, express by itself does not. That's why you can use a module such as formidable or multer that read the format multipart/form-data

you just need

$ npm install formidable --save
## o yarn add formidable

and in your routes.js you add the following

// routes.js
// todas tus dependencias aquí
const formidable= require("formidable");

routes.post("/contact",(req,res)=>{
    const form = new formidable.IncomingForm();

   form.parse(req, (err, formFields, formFiles) => {
     const { inputemail, inputcoment } = formFields
    pool.query("INSERT INTO contact (email, info) VALUES (?, ?)", [inputemail, inputcoment],
        function (error, results, fields) {
            if (error) { throw error }
            if (results) { console.log(results); }
        }
    );
    /* 
      si esperas mandar index.html
      en base al resultado de la 'query'
      los tienes que mandar dentro de la
      'callback' de 'pool.query'
    */
    res.render("index.html");
    });
});

Edited

There is also an error with your form you are declaring

<textarea id="areacoment" name="areacoment" />

For that reason the query does not run correctly because inputcoment is not defined anywhere in the table, so if you want to use the name="inputcoment" you have to modify your <textarea> to make it look like this

<textarea id="inputcoment" name="inputcoment"></textarea>

and that way you can read the variable.

One last comment is that the correct way to write it would be inputcomment

    
answered by 25.12.2018 / 00:46
source