First, apologies because I couldn't figure out a better title for this question. I am gonna try to explain my issue, let me know if you can't understand what is going on.
When deleting a movie, I also would like to delete the id of the movies from its genres arrays. Each Genre has a movie array with all the movie ids in that genre and each movie has a genre array with all the genre ids of that movie.
async delete(req, res, next) {
try {
const movie = await Movie.findOne({ _id: req.params.id });
if (!movie) return res.status(400).send({ error: "Movie not found" });
// Here I find all the genres. I get an array with all the genres objects
// [{genre1}, {genre2}] inside the genre object there is an array with all the movie ids that belongs to that genre
let _genres = await Genre.find({ _id: { $in: movie.genre } });
// Now I am trying to return all the genre that are different from the movie._id I am deleting
_genres = await Promise.all(_genres.map(async (genre) => {
genre.movies.filter(item => item !== movie._id);
await genre.save();
return genre;
}));
await movie.remove();
return res.send({ deleted: true });
} catch (e) {
next(e);
}
}
I am not getting any error, but it is not deleting the movie id from the genre. Thanks.