0

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); 
    });
});
4
  • Well it's a pretty consice error as I don't need robots being "declared" anywhere. Surely you have more code than this. You should be calling somthing like robots = mongoose.model("robots"), and also after an initial declaration of that model as well. Commented Sep 9, 2015 at 7:09
  • I do have a model, which contains a schema, But where can I declare it so it links to my db, I don't precisely understand where I can declare my db on the route or server. Commented Sep 9, 2015 at 7:15
  • First you "need to " declare the model, which you seemingly already have somewhere. But the specific problem here is that you have declared this in another module and therefore "must" require that module to import the model definition or alternately call mongoose.model "again" with the same model name as defined in your local scope. The error being reported is because their is no object declared in the local scope. Commented Sep 9, 2015 at 7:21
  • That fixed the robot undefined error, but not I get an error on the same line, Robot.remove({_id: mongoose.ObjectId(id)}).exec(function(err, result) Will something like this execute the code or how would I reword it. btw thanks you've been helpful. Commented Sep 9, 2015 at 7:25

1 Answer 1

4

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.