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.