Mongoose Modeling Relationships: Project 6 Movies

So, I’m wondering why he made it so we have to enter a _id in the post request inside movies.js? when testing the post using thunder client in vs code.

Manually adding a ID can be tedious. I wanted to try and automatically give the movies object genra a uuid upon creation. But, having a hard time getting this to work.

I installed the npm package uuid, required it and tried calling the function inside the post request below.

const { v4: uuidv4 } = require("uuid");  

router.post("/", async (req, res) => {

  const { error } = validateMovies(req.body);

  if (error) return res.status(400).send(error.details[0].message);

  const genre = await Genra.findById(req.body.genreId);

  if (!genre) return res.status(400).send("Invalid Genre.");

  let movie = new Movies({
    title: req.body.title,
    genre: {
      _id: uuidv4() , // tried calling the function here to randomly create a uuid....Doesn't work
      name: genre.name,
    },

    numberInStock: req.body.numberInStock,
    dailyRentalRate: req.body.dailyRentalRate,
  });

  movie = await movie.save();
});

I would rather have the genre: {_id: genre._id} be automated upon the post request using the npm package I installed but, currently have no idea how to do this with my current knowledge of node and javascript ect.

The thing also that is confusing is, that mongodb does automate a _id for every data in a collection so this confusing me that we have to manually enter in a _ui in the movies post request body json…

I reverted my movies post function back to what mosh had it and tried to do a posts test using thunder client. I get an error 400 bad request and the response is res.send(movies).

router.post("/", async (req, res) => {

  const { error } = validateMovies(req.body);

  if (error) return res.status(400).send(error.details[0].message);

  const genre = await Genra.findById(req.body.genreId);

  if (!genre) return res.status(400).send("Invalid Genre.");

  let movie = new Movies({

    title: req.body.title,

    genre: {

      _id: genre._id,

      name: genre.name,

    },

    numberInStock: req.body.numberInStock,

    dailyRentalRate: req.body.dailyRentalRate,

  });

  movie = await movie.save();

});

What would the post function look like if I just wanted to create movie with only the name of a genre thats already created? and have it just match the _id of the genre already in the database.

or

What would the post function look like if I wanted to auto generate a new _ui for the movies genre reference using uuid npm package?

Update: Came across some typos and a totally missed line. If will edit this post some more if I don’t fix the issue if I do will just close the thread.

I wish MOSH would have showed posting a movie using postman, thunderclient or anything so I know what to enter in the body/json.

If you want to look for a name instead of const genre = await Genre.findById(req.body.genreId); you’ll have to look for a genre by name, which would probably look something like this:
const genre = await Genre.findOne({"name": name})

If you just want to test your post request in postman with a valid _id, you can always go into your database with Mongo Compass and just copy paste a valid genre _id from there.

Why would you want to generate new UUID that points to nothing? Do you want to create a new genre from the name only? If so, then just copy paste the code from router.post("/") in genres.js

There’s a schema validation function and a schema in the movie model. Stick to those and send a post request with all the required fields in your request body to the post endpoint. That’s what the models are for, so you can always go back and look what your movie data has to look like.