title: UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError [ERR_ASSERTION]
body: <p>I have the following setup in Visual Studio Code for nodejs project:</p>
  
  <p>Model: genre.js</p>
  
  <pre><code>const Joi = require('joi');
  const mongoose = require('mongoose');
  
  const genreSchema = new mongoose.Schema({
    name:{
        type:String,
        required:true,
        minlength:5,
        maxlength:50
    }
  });
  
  const Genre = mongoose.model('Genre',genreSchema);
  
  function validateGenre(genre) {
      const schema = {
          name: Joi.string().min(3).required()
      };
  
      return Joi.validate(genre, schema);
  }
  
  exports.genreSchema = genreSchema;
  exports.Genre = Genre;
  exports.validate = validateGenre;
  </code></pre>
  
  <p>movie.js:</p>
  
  <pre><code>const Joi = require('joi');
  const mongoose = require('mongoose');
  const { genreSchema } = require('./genre');
  
  const Movie = mongoose.model('Movie', new mongoose.Schema({
      title: {
          type: String,
          required: true,
          trim: true,
          minlength: 5,
          maxlength: 255
      },
      genre: {
          type: genreSchema,
          required: true
      },
      numberInStock: {
          type: Number,
          required: true,
          min: 0,
          max: 255
      },
      dailyRentalRate: {
          type: Number,
          required: true,
          min: 0,
          max: 255
      }
  }));
  
  function validateMovie(movie) {
      const schema = {
          title: Joi.string().min(5).max(50).required,
          genreId: Joi.string().required,
          numberInStock: Joi.number().min(0).required,
          dailyRentalRate: Joi.number().min(0).required
      };
  
      return Joi.validate(movie,schema);
  }
  
  exports.Movie = Movie;
  exports.validate = validateMovie;
  </code></pre>
  
  <p>routes: movies.js</p>
  
  <pre><code>const { Movie, validate } = require('../models/movie');
  const { Genre } = require('../models/genre');
  const mongoose = require('mongoose');
  const express = require('express');
  const router = express.Router();
  
  
  router.get('/', async (req, res) =&gt; {
      const movies = await Movie.find().sort('name');
      res.send(movies);
  });
  
  router.get('/:id', async (req, res) =&gt; {
      const movie = await Movie.findById(req.params.id);
      if (!movie) return res.status(404).send('The movie with the given Id was not found');
      res.send(movie);
  });
  
  router.post('/', async (req, res) =&gt; {
      const { error } = validate(req.body);
      if (error) return res.status(400).send(error.details[0].message);
  
      const genre = await Genre.findById(req.body.genreId);
      if (!genre) return res.status(404).send('Invalid genre.');
  
      let movie = new Movie({
          title: req.body.title,
          genre: {
              _id: genre._id,
              name: genre.name
          },
          numberInStock: req.body.numberInStock,
          dailyRentalRate: req.body.dailyRentalRate
      });
  
      movie = await movie.save();
      res.send(movie);
  });
  
  router.put('/:id', async (req, res) =&gt; {
      const { error } = validate(req.body);
      if (error) return res.status(400).send(error.details[0].message);
  
      const genre = await Genre.findById(req.body.genreId);
      if (!genre) return res.status(404).send('Invalid genre.');
  
      const movie = await Movie.findByIdAndUpdate(req.params.id, {
          title: req.body.title,
          genre: {
              _id: genre._id,
              name: genre.name
          },
          numberInStock: req.body.numberInStock,
          dailyRentalRate: req.body.dailyRentalRate
      }, { new: true });
  
      if (!movie) return res.status(404).send('The movie with the given ID was not found.');
      res.send(movie);
  });
  
  router.delete('/:id', async (req, res) =&gt; {
      const movie = await Movie.findByIdAndRemove(req.params.id);
      if (!movie) return res.status(404).send('The movie with the given ID was not found.');
      res.send(movie);
  });
  
  module.exports = router;
  </code></pre>
  
  <p>On trying to create a movie with the following setup :</p>
  
  <p><a href="https://i.stack.imgur.com/cIDlz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cIDlz.jpg" alt="enter image description here"></a></p>
  
  <p>I am getting an error:</p>
  
  <p>UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError [ERR_ASSERTION]</p>
  
  <p><a href="https://i.stack.imgur.com/oEFH6.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oEFH6.jpg" alt="enter image description here"></a></p>
  
  <p>Can anyone help me to fix this issue</p>