node.js - 通过 mongodb 和 mongoose 中的 id 删除对象

标签 node.js mongodb mongoose

我在 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/

相关文章:

javascript - 查找文档,然后在数组中查找条目并将其从数组中删除

PHP/MongoDB : update a value in an array

node.js - 方法 "collection.find()"最多接受两个参数

javascript - 在html文件中显示存储在MongoDb中的图像

javascript - 从 fetch 中获取字符串?

node.js - 如何找出 Node.js 进程的 % CPU 使用率?

javascript - JS (node.js) - 如果我进行异步调用但不等待它完成,是否可以保证完成执行?

java - Mongodb 读取查询使用 Spring Data 非常慢,而使用 Morphia 则速度很快

mongodb - 如何使用 $indexOfArray 返回数组索引

node.js - Mongoose :不允许更新特定字段