0

I am trying to make a post request to the server (mongodb) but I get this error:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'todo_description' of undefined

I am running mongodb on my localhost

// Require Express
const express = require("express");

// Setting Express Routes
const router = express.Router();

// Set Up Models
const Todo = require("../models/todo");

// Get All Todos
router.get("/", async (req, res) => {
  try {
    const todo = await Todo.find();
    res.json(todo);
  } catch (err) {
    res.json({ message: err });
  }
});

router.get("/:id", async (req, res) => {
  try {
    const id = req.params.id;
    await Todo.findById(id, (err, todo) => {
      res.json(todo);
    });
  } catch (err) {
    res.json({ message: err });
  }
});

router.post("/add", async (req, res) => {
  const todo = new Todo({
    todo_description: req.body.todo_description,
    todo_responsible: req.body.todo_responsible,
    todo_priority: req.body.todo_priority,
    todo_completed: req.body.todo_completed,
  });

  try {
    await todo.save();
    res.json(todo);
  } catch (err) {
    res.json({ message: err });
  }
});

router.patch("/update/:id", async (req, res) => {
  try {
    const updateTodo = await Todo.updateOne(
      { _id: req.params.id },
      { $set: { todo_description: req.body.todo_description } }
    );
    updateTodo.save().then(updateTodo => {
      res.json(updateTodo);
    });
  } catch (err) {
    res.json({ message: err });
  }
});

router.delete("/delete/:id", async (req, res) => {
  try {
    const deleteTodo = await Todo.deleteOne({ _id: req.params.id });
    res.json(deleteTodo);
  } catch (err) {
    res.json({ message: err });
  }
});

module.exports = router;

my todo model

// Require Mongoose
const mongoose = require("mongoose");

// Define Schema
// const Schema = new mongoose.Schema;

// Define Todo-Schema
const TodoSchema = new mongoose.Schema({
  // Creating Fields
  todo_description: {
    type: String
  },
  todo_responsible: {
    type: String
  },
  todo_priority: {
    type: String
  },
  todo_completed: {
    type: Boolean
  },
  todo_date: {
    type: Date,
    default: Date.now
  }
});

// Compile Model From Schema
// const TodoModel = mongoose.model("Todos", TodoSchema);

// Export Model
module.exports = mongoose.model("todos", TodoSchema);

error message:

(node:548) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'todo_description' of undefined at router.post (C:\Users\kinG\Desktop\projects\mountain-of-prototype\mern\backend\routes\todo.js:33:32) at Layer.handle [as handle_request] (C:\Users\kinG\Desktop\projects\mountain-of-prototype\mern\backend\node_modules\express\lib\router\layer.js:95:5)

thank you

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
KinG
  • 13
  • 4
  • Do you use body-parser? – Matin Sasan Jun 11 '19 at 14:32
  • yes trying the get method says this Cannot set headers after they are sent to the client hope u dont mind the questions am new to backend – KinG Jun 11 '19 at 15:36
  • Sorry, that's an old comment of mine, older than the answer given, which already solved your problem. And are you talking about a new error now btw? Then it's time to start a new question then. Be sure to include all the related code to your new question (So never ask for two or more things in one question. Each question made has one only one aim - just trying to help you avoid downvote from others). Welcome here, and don't let some potential negative events discourage you - esp. be very careful how you question and answer (relax, you'll get the hang of it). Wish you all the best, mate. – Matin Sasan Jun 11 '19 at 15:47

2 Answers2

1

You are accessing todo_description from req.body. req.body will only be available if you add the body-parser middleware or add a similar one yourself.

Add this right before your routes are loaded :

const bodyParser = require('body-parser');
const express = require('express');

const app = express();

app.use(bodyParser.json());

You can also add this to a specific route. Read more about it here.

Easwar
  • 5,132
  • 1
  • 11
  • 21
0

You should use body-parser in your master file of the application. Which gives you the parsed json before your middle-ware parse the body, which by-default in string. And also make sure you are sending todo_description in the req.body(should check before use).

const bodyParser = require('body-parser');
app.use(bodyParser.json());