I'm working on my own project. His main idea is movie reviews.
I'm currently working on the HTTP requests trying to make my code a little better. I guess I have a problem with my promise handling:
try {
let newMovie = await MovieRepository.addNewMovie({
name: req.body.name,
year: req.body.year,
description: req.body.description,
length: req.body.length,
genres: req.body.genres,
totalRating: req.body.totalRating
});
return res.status(200).json({ name: newMovie, message: 'Movie added successfully!' });
} catch (error) {
return res.status(400).json({ status: 400, message: errorMessage.addNewMovie });
}
This code works! I'm adding a new Movie and I get an OK status as expected.
Quick explaining about my code: I'm using MovieRepository
where I use mongoose functionality to add a new movie. get all the info I need from the body and send it to the client.
Now, I've tried to change my code a little bit :
let newMovie = await MovieRepository.addNewMovie({
name: req.body.name,
year: req.body.year,
description: req.body.description,
length: req.body.length,
genres: req.body.genres,
totalRating: req.body.totalRating
}).then(() => {
return res.status(200).json({
name: newMovie,
message: 'Movie added successfully!'
});
}).catch((error) => {
return res.status(400).json({
status: 400,
message: errorMessage.addNewMovie,
systemError: error
});
});
This code is not working well.
Here I'm using the promise abilities to use then
and catch
. Pay attention that I didn't actually changed anything inside those functionalities.
In this case, the movie added to the database successfully as well, but the client gets status 400.
Any ideas what's happening here?