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
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 afterconst app=express();
;it needs reorder codes