Resolving UnhandledPromiseRejectionWarning in express post request

爷,独闯天下 提交于 2019-12-08 11:35:05

问题


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


回答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.



来源:https://stackoverflow.com/questions/56545690/resolving-unhandledpromiserejectionwarning-in-express-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!