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

时间:2015-09-09 07:05:10

标签: node.js mongodb mongoose

我在机器人上收到错误。删除说明机器人没有定义,但我无法弄明白为什么。请帮忙。谢谢。

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 个答案:

答案 0 :(得分:4)

您必须先加载用户模型。

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

robots.js文件应如下所示:

var mongoose = require('mongoose');

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

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