title: Node.js / MongoDB - TypeError: Cannot destructure property 'title' of 'req.body' as it is undefined
body: <p>I am preparing a project for an interview where I need to build an app to display videos to logged in users. I've started working on the back-end (in Node.js / Express / TypeScript / Mongoose / MongoDB). Everything worked until I tested posting data in Thunder Client; I got this error message:</p>
  <pre><code>TypeError: Cannot destructure property 'title' of 'req.body' as it is undefined.
      at createVideo (/home/wilder/Bureau/video-streaming-server/src/controllers/Video.ts:8:5)
      at Layer.handle [as handle_request] (/home/wilder/Bureau/video-streaming-server/node_modules/express/lib/router/layer.js:95:5)
      at next (/home/wilder/Bureau/video-streaming-server/node_modules/express/lib/router/route.js:144:13)
      at Route.dispatch (/home/wilder/Bureau/video-streaming-server/node_modules/express/lib/router/route.js:114:3)
      at Layer.handle [as handle_request] (/home/wilder/Bureau/video-streaming-server/node_modules/express/lib/router/layer.js:95:5)
      at /home/wilder/Bureau/video-streaming-server/node_modules/express/lib/router/index.js:284:15
      at Function.process_params (/home/wilder/Bureau/video-streaming-server/node_modules/express/lib/router/index.js:346:12)
      at next (/home/wilder/Bureau/video-streaming-server/node_modules/express/lib/router/index.js:280:10)
      at Function.handle (/home/wilder/Bureau/video-streaming-server/node_modules/express/lib/router/index.js:175:3)
      at router (/home/wilder/Bureau/video-streaming-server/node_modules/express/lib/router/index.js:47:12)
  </code></pre>
  <p>I know very similar questions have been asked (and answered here) but I cannot make it work still. As I gathered from said questions and answers, the content of req.body is, by default, undefined, hence I tried using</p>
  <pre><code>app.use(express.json());
  </code></pre>
  <p>but it doesn't change anything.</p>
  <p>Here is my code so far:</p>
  <p>src/Server.ts</p>
  <pre><code>import express from &quot;express&quot;;
  import http from &quot;http&quot;;
  import mongoose from &quot;mongoose&quot;;
  import { config } from &quot;./config/config&quot;;
  import videoRoutes from &quot;./routes/Video&quot;;
  
  const router = express();
  
  // Connect to MongoDB
  mongoose
    .connect(config.mongo.url, { retryWrites: true, w: &quot;majority&quot; })
    .then(() =&gt; {
      console.log(&quot;Connected to MongoDB.&quot;);
      startServer();
    })
    .catch((err) =&gt; {
      console.log(err);
    });
  
  // Start server (only if MongoDB connects)
  const startServer = () =&gt; {
    // Routes
    router.use(&quot;/videos&quot;, videoRoutes);
  
    // Error handling
    router.use((req, res) =&gt; {
      const err = new Error(&quot;not found&quot;);
      console.error(err);
  
      return res.status(404).json({ message: err.message });
    });
  
    // Server running
    http
      .createServer(router)
      .listen(config.server.port, () =&gt;
        console.log(`Server is running on port ${config.server.port}.`)
      );
  };
  </code></pre>
  <p>src/models/Video.ts</p>
  <pre><code>import mongoose, { Document, Schema } from &quot;mongoose&quot;;
  
  export interface IVideo {
    title: string;
    description: string;
    video_path: string;
    tags: string[];
    duration: string;
    views: string;
    creator: string;
    created_at: string;
    like: number;
    dislike: number;
    comments: [
      {
        user: string;
        text: string;
        date: string;
      }
    ];
  }
  
  const VideoSchema: Schema = new Schema(
    {
      title: { type: String, required: true },
      description: { type: String, required: true },
      video_path: { type: String, required: true },
      tags: { type: Array, required: true },
      duration: { type: String, required: true },
      views: { type: String, required: true },
      creator: { type: String, required: true },
      created_at: { type: String, required: true },
      like: { type: Number, required: true },
      dislike: { type: Number, required: true },
      comments: [
        {
          user: { type: String, required: true },
          text: { type: String, required: true },
          date: { type: String, required: true },
        },
      ],
    },
    { versionKey: false }
  );
  
  export default mongoose.model&lt;IVideo&gt;(&quot;Video&quot;, VideoSchema);
  </code></pre>
  <p>src/controllers/Video.ts</p>
  <pre><code>import { NextFunction, Request, Response } from &quot;express&quot;;
  import mongoose, { Mongoose } from &quot;mongoose&quot;;
  import Video from &quot;../models/Video&quot;;
  
  // Create video
  const createVideo = (req: Request, res: Response, next: NextFunction) =&gt; {
    const {
      title,
      description,
      video_path,
      tags,
      duration,
      views,
      creator,
      created_at,
      like,
      dislike,
      comments,
      user,
      text,
      date,
    } = req.body;
  
    const video = new Video({
      _id: new mongoose.Types.ObjectId(),
      title,
      description,
      video_path,
      tags,
      duration,
      views,
      creator,
      created_at,
      like,
      dislike,
      comments,
      user,
      text,
      date,
    });
  
    return video
      .save()
      .then((video) =&gt; res.status(201).json({ video }))
      .catch((err) =&gt; res.status(500).json({ err }));
  };
  
  // Get a single video
  const readVideo = (req: Request, res: Response, next: NextFunction) =&gt; {
    const videoId = req.params.videoId;
  
    return Video.findById(videoId)
      .then((video) =&gt;
        video
          ? res.status(200).json({ video })
          : res.status(404).json({ message: &quot;Not found.&quot; })
      )
      .catch((err) =&gt; res.status(500).json({ err }));
  };
  
  // Get all videos
  const readAll = (req: Request, res: Response, next: NextFunction) =&gt; {
    return Video.find()
      .then((videos) =&gt; res.status(200).json({ videos }))
      .catch((err) =&gt; res.status(500).json({ err }));
  };
  
  // Update video
  const updateVideo = (req: Request, res: Response, next: NextFunction) =&gt; {
    const videoId = req.params.videoId;
  
    return Video.findById(videoId)
      .then((video) =&gt; {
        if (video) {
          video.set(req.body);
  
          return video
            .save()
            .then((video) =&gt; res.status(201).json({ video }))
            .catch((err) =&gt; res.status(500).json({ err }));
        } else {
          res.status(404).json({ message: &quot;Not found.&quot; });
        }
      })
      .catch((err) =&gt; res.status(500).json({ err }));
  };
  
  // Delete video
  const deleteVideo = (req: Request, res: Response, next: NextFunction) =&gt; {
    const videoId = req.params.videoId;
  
    return Video.findByIdAndDelete(videoId)
      .then((video) =&gt;
        video
          ? res.status(201).json({ message: &quot;Deleted.&quot; })
          : res.status(404).json({ message: &quot;Not found.&quot; })
      )
      .catch((err) =&gt; res.status(500).json({ err }));
  };
  
  export default {
    createVideo,
    readVideo,
    readAll,
    updateVideo,
    deleteVideo,
  };
  </code></pre>
  <p>src/routes/Video.ts</p>
  <pre><code>import express from &quot;express&quot;;
  import controller from &quot;../controllers/Video&quot;;
  
  const app = express();
  const router = express.Router();
  
  app.use(express.json());
  
  router.post(&quot;/create&quot;, controller.createVideo);
  router.get(&quot;/get/:videoId&quot;, controller.readVideo);
  router.get(&quot;/get/&quot;, controller.readAll);
  router.patch(&quot;/update/:videoId&quot;, controller.updateVideo);
  router.delete(&quot;/delete/:videoId&quot;, controller.deleteVideo);
  
  export = router;
  </code></pre>
  <p>And here is the format of data I need to add in my database</p>
  <pre><code>{
      &quot;title&quot;: &quot;Rotating planet&quot;,
      &quot;description&quot;: &quot;Vidéo de la planète en train de tourner&quot;,
      &quot;video&quot;: &quot;/video_example.mp4&quot;,
      &quot;tags&quot;: [&quot;planet&quot;, &quot;earth&quot;, &quot;science&quot;, &quot;space&quot;],
      &quot;duration&quot;: &quot;00:31&quot;,
      &quot;views&quot;: 19876,
      &quot;creator&quot;: &quot;ThePlanetGuy&quot;,
      &quot;created_at&quot;: &quot;2021-05-01T08:00:00.00Z&quot;,
      &quot;like&quot;: 10,
      &quot;dislike&quot;: 2,
      &quot;comments&quot;: [{
          &quot;user&quot;: &quot;Anonymous&quot;,
          &quot;text&quot;: &quot;Beautiful&quot;,
          &quot;date&quot;: &quot;2021-05-10T08:00:00.00Z&quot;
      }, {
          &quot;user&quot;: &quot;Flatter&quot;,
          &quot;text&quot;: &quot;Fake ! The earth is flat !&quot;,
          &quot;date&quot;: &quot;2021-05-05T10:00:00.00Z&quot;
      }]
  }
  </code></pre>
  <p>If anyone could help me understand what I did wrong, I would gladly appreciate it. I am still very new to programming, especially back-end.</p>
  <p>Thank you :)</p>