Getting Type error Cannot read propertiesof undefeined - Node, Express, Mongoose

Viewed 22
Index.js (App)
I called controller in the index.fs which is todoController in my case 
const express = require('express'); //start the server
const app = express(); //start the server
var todoController = require('./controllers/todoController');

app.use("/static", express.static("public"));
app.use(express.static('./public')); //static files
todoController(app); //fire controllers
app.use(express.urlencoded({ extended: true })); //URLencoded allows data extraction from the form by adding it to the body property of the request
app.listen(5000, () => console.log("Server Up and running")); //tell express app to listen to port 3000
app.set("view engine", "ejs"); //view engine configuration 


todoController.js (Controller)
const dotenv = require('dotenv'); //connect to the database
const mongoose = require("mongoose"); //Mongoose provides a straight-forward, schema-based solution to model your application data.
const TodoTask = require("../models/TodoTask");

dotenv.config(); //connect to the database

//run server only after the connection is made
mongoose.connect('mongodb+srv://bhupesh:1234@cluster0.7at15.mongodb.net/myFirstDatabase?retryWrites=true&w=majority',{useNewUrlParser: true,useUnifiedTopology: true }, () => {
console.log("Connected to db!");
});

module.exports=function(app){
app.get("/", (req, res) => {
    TodoTask.find({}, (err, tasks) => {
    res.render("todo.ejs", { todoTasks: tasks });
    });
});


// app.post('/',(req, res) => {
//     console.log(req.body);
//     });

//POST METHOD
app.post('/',async (req, res) => {
    const todoTask = new TodoTask({
    content: req.body.content
    });
    try {
    await todoTask.save();
    res.redirect("/");
    } catch (err) {
    res.redirect("/");
    }
});
}

I'm getting TypeError in the post method at variable 'content' how do I resolve this, I tried all possible ways

//app.listen(3000, () => console.log("Server Up and running"));

TodoTask.js (model)
//collection schema and 
//we exported so we could use it at index.js file

const mongoose = require('mongoose');
const todoTaskSchema = new mongoose.Schema({
content: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
})
module.exports = mongoose.model('TodoTask',todoTaskSchema);

I defined content in the model todoTask.js

TypeError: Cannot read properties of undefined (reading 'content') at C:\Users\Sathish\OneDrive\Desktop\TodoApp-master\controllers\todoController.js:27:23 at Layer.handle [as handle_request] (C:\Users\Sathish\OneDrive\Desktop\TodoApp-master\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\Sathish\OneDrive\Desktop\TodoApp-master\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\Sathish\OneDrive\Desktop\TodoApp-master\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\Sathish\OneDrive\Desktop\TodoApp-master\node_modules\express\lib\router\layer.js:95:5) at C:\Users\Sathish\OneDrive\Desktop\TodoApp-master\node_modules\express\lib\router\index.js:281:22

1 Answers

You're body is empty because it doesn't know how to parse it. You should use the middleware from express: app.use(express.json()), maybe right after app.use(express.urlencoded({ extended: true }));

The urlencoded let's you to send data from a form but if you're testing using Postman, for example, you need to parse it as JSON

Related