0

I'm using nodeJS MongoDB/Mongoose to create/update/delete movies inside the database using Postman post/delete methods. The create function is working fine, and even the remove function is working properly so when I use Postman I get the return: "Movie has been deleted!" like it should.

The only problem is that my function is emptying the entire database of movies instead of just that 1 movie, here is the remove function:

 function destroy(req, res, next){
    var movieID = req.body
            Movie.remove(movieID, function(err,movie){
                    if(err){
                        res.status(400).send(err)
                    } else {
                        res.send("Movie has been deleted!")
                        db.close()
                    }
            })

The movie object:

var movieSchema = new mongoose.Schema({
    name: String,
    yay: Number,
    nay: Number,
    release_date: Date,
    in_theaters: Boolean,
    released: Boolean,
    buy_link: String,
    imdb_link: String,
    image_url: String,
    description: String,
    trailer_link: String
})

I want to delete a movie based on it's "name" so I only have to input the name and it will delete the entire movie.

7
  • Is this correct var movieID = req.body ? I think it might be like this: var movieID = req.body.movieID; Commented May 19, 2016 at 9:15
  • Before db.close(), add one line console.log(movie); if movie contains n : 1, then it removed 1 record from DB. it it is 0, then nothing happened. Commented May 19, 2016 at 9:17
  • Alternatively, you can enable debug mode of mongoose, mongoose.set('debug', true); which prints the every query to mongodb. Commented May 19, 2016 at 9:18
  • Yeah it responds to the remove function because when I get the entire index of the movieDB it's empty. I tried what you said as well and it said 1. Commented May 19, 2016 at 9:19
  • @HirenS. Thanks for the suggestions, I'm gonna try them! Commented May 19, 2016 at 9:24

3 Answers 3

1

Have you tried the findOneAndRemove query?

This query is much cleaner compared to finding a model and removing it inside the callback. Beside this I assume it's faster because you basically do 1 query instead of 2 after each other.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are passing direct value to Remove method, it will try to match with _id field.

As per your model, _id is ObjectId field which is managed automatically by mongodb. In case if you enter like this. .remove("movie", callback) which is not a valid ObjectId.

Mongoose is discarding this invalid condition and executing Movie.remove({}); which is deleting all your records.

So it is better to validate whether the input is valid ObjectId or not before directly passing to Movie.remove();

I also recommend to use like this: Movie.remove({_id: movieId}, callback). And for movie name :

Movie.remove({name: movieName}, callback);

Update: You can take from Postman

var movieName = req.body.movieName;

Movie.remove({name: movieName}, function(err, updateObj){
});

Comments

0

Can you try this?

    var movieName = req.body.name;
    Movie.find('name': movieName, function(err, movie) {
        if (err) res.send({error: err});


        Movie.remove(function(err, movie){
            if (err) res.send({error: err});
            res.json({message: "Movie is removed", movie: movie});
        });

    });

4 Comments

Tried it, this also deletes all the movies inside the database.
I updated my answer. Could you please test that as well?
Tried it, it gives me a "not found" back when I use postman to do the following: { "name": "Captain America: Civil War" }
I'll try to fix it when I get home. Could you please share the way you call the destroy() function?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.