通过mongodb和mongoose中的id删除对象

时间:2022-03-09 23:35:23

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); 
    });
});

1 个解决方案

#1


3  

You have to load the user model first.

您必须先加载用户模型。

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

robots.js file should look like this:

robots.js文件应如下所示:

var mongoose = require('mongoose');

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

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

#1


3  

You have to load the user model first.

您必须先加载用户模型。

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

robots.js file should look like this:

robots.js文件应如下所示:

var mongoose = require('mongoose');

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

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