James Lopez
James Lopez

Reputation: 49

Remove an object by an id in mongodb and mongoose

I'm getting an error on robots.remove stating robots is not defined, But I can't possibly figure out exactly why. Please help. thank you.

mongoose.connect('mongodb://localhost/robots'); //connecting to localdb

router.delete('/:id', function(req,res){ 

    var id = req.params.id;
    console.log(id);

    robots.remove({_id:ObjectId(id)}, function(err, result){ //undefined??
        if (err) return res.status(500).send({err: 'Error: Could not delete robot'});
        if(!result) return res.status(400).send({err: 'Robot bot deleted from firebase database'});
        console.log('deleted!!!');
        res.send(result); 
    });
});

Upvotes: 0

Views: 2972

Answers (1)

shan1024
shan1024

Reputation: 1399

You have to load the user model first.

var robots  = require('../app/models/robots');//Load the model

robots.js file should look like this:

var mongoose = require('mongoose');

var robotSchema = mongoose.Schema({
//Your schema here
});

module.exports = mongoose.model('robots', robotSchema);

Upvotes: 4

Related Questions