我在 robots.remove 上收到错误,指出 robots 未定义,但我无法弄清楚到底为什么。请帮忙。谢谢。
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);
});
});
最佳答案
您必须首先加载用户模型。
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);
关于node.js - 通过 mongodb 和 mongoose 中的 id 删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32472955/