Genre is not allowed when post movie object to mongodb

Hello guys

I followed along to chapter 9 Mongoose- Modeling Relationships between Connected Data and lesson 6 build the movie API.

The get method works and returns an empty array because there is no document in the new collection. When I post a new JSON object like this below

Already spent days stuck on this… Thanks for any ideas. Cheers

Only the variables defined in the Joi schema will be allowed. If the post body contains any extra fields, the entire request will be dismissed.

In this case, you’re sending a nested genre object in your post body, but in the Joi schema you’ve not defined genre . On the other hand, you’ve defined genreId which is not present in the post body.

You can do one of two things.

  1. Remove genreId and define genre in your Joi schema as follows
genre: Joi.object().keys({
    _id: Joi.string(),
    name: Joi.string()
  1. [Recommended approach] Change your post body to match your Joi schema. Instead of the genre object, send the genreId as a string.
"title": "007 No Time to Die",
"numberInStock": 10,
"dailyRentalRate": 5,
"genreId": "id_stored_in_your_database"

The option you choose depends upon your implementation of the API.

1 Like

Hey @Spark,

Thanks for sharing this solution! I was really confused about the same thing.