Search code examples
node.jsmongodbmongoosepostpostman

Post Request not updating header info in MongoDB using Nodejs


I am trying to write a very basic POST request that stores a reminder and the status of the reminder using Mongoose. e.g. {"name":"Drink Water", "completed":false}

While testing the API on Postman the given POST request only enters the __id and _v in the database, the header info is not updated. Kindly look into the code and help me correct it.

Model

const mongoose = require("mongoose");

const TaskSchema = new mongoose.Schema({
  name: String,
  completed: Boolean 
});

module.exports = mongoose.model("Task", TaskSchema);

Task Controller

const Task = require("../models/Task");

const getAllTasks = (req, res) => {
  res.send("Get all Available Tasks");
};
const createTask = async (req, res) => {   // code for POST req.
  const task = await Task.create(req.body);
  res.status(201).json({ task });
  
};



module.exports = {
  getAllTasks,
  createTask,
 
};

Testing on Postman


Solution

  • You need to apply express.json() middleware to be able to read request body.

    Can you add the following line to your main file before routes?

    app.use(express.json());