skip to Main Content

I’m having an issue with the submission of the information from this form I created with Node.js. Can someone please help me? I’m receiving the following error:

TypeError: Cannot read properties of undefined (reading 'conteudo')

my code

const express=require("express");
const app=express();
const handlebars=require("express-handlebars");
const Sequelize=require("sequelize");


const sequelize=new Sequelize("test","root","88182231",{
    host:"localhost",
    dialect:"mysql"
})

app.get("/card",function(req,res){
    res.render("form")
})

app.post("/add",function(req,res){
     res.send("conteudo:"+req.body.conteudo+" titulo:"+req.body.titulo)
})

app.engine("handlebars", handlebars.engine({ defaultLayout: "main" }));
app.set("view engine","handlebars");
app.use(express.urlencoded({extended:false}))
app.use(express.json())

I have already tried changing my database, even the data type from TEXT to STRING, and nothing.

2

Answers


  1. You have to use express.json() middleware. It parses incoming requests with JSON payloads and you can access the passed data using req.body.

    Learn more: https://expressjs.com/en/api.html#express.json

    so add app.use(express.json()) somewhere after const app=express();;

    Login or Signup to reply.
  2. it needs reorder codes

    const express=require("express");
    const app=express();
    const handlebars=require("express- 
    handlebars");
    const Sequelize=require("sequelize");
    
     app.engine("handlebars", handlebars.engine({ 
     defaultLayout: "main" }));
    app.set("view engine","handlebars");
    app.use(express.urlencoded({extended:false}))
    app.use(express.json())
    
    const sequelize=new 
    Sequelize("test","root","88182231",{
    host:"localhost",
    dialect:"MySQL"
     })
    
     app.get("/card",function(req,res){
     res.render("form")
       })
    
    app.post("/add",function(req,res){
      res.send("conteudo:"+req.body.conteudo+" 
    titulo:"+req.body.titulo)
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search