如何删除特定数据 mongodb + nodejs + expressjs

How to delete specific data mongodb + nodejs + expressjs

提问人:softboxkid 提问时间:7/20/2014 最后编辑:Stenniesoftboxkid 更新时间:9/6/2018 访问量:11303

问:

如何使用 node+express 删除数据。这是我的路由器删除过程

// Delete user
router.get('/deleteuser/:id', function(req, res) { 
var db = req.db;

var uid = req.params.id;
db.collection('usercollection').remove(uid, function(err, result) { 
    res.send( (result === 1) ? { msg: '' } : { msg: 'error: '+ err } );
});

});

从上面的功能中,我得到了 404 错误页面。

Error: Not Found
at Layer.app.use.res.render.message [as handle] (/media/bcd44b6a-9c2e-4496-81da-b45d3c349c91/mywork/nodejs/blog/blogone/app.js:40:15)
at trim_prefix (/media/bcd44b6a-9c2e-4496-81da-b45d3c349c91/mywork/nodejs/blog/blogone/node_modules/express/lib/router/index.js:240:15)
at /media/bcd44b6a-9c2e-4496-81da-b45d3c349c91/mywork/nodejs/blog/blogone/node_modules/express/lib/router/index.js:208:9
at Function.proto.process_params (/media/bcd44b6a-9c2e-4496-81da-b45d3c349c91/mywork/nodejs/blog/blogone/node_modules/express/lib/router/index.js:269:12)
at next (/media/bcd44b6a-9c2e-4496-81da-b45d3c349c91/mywork/nodejs/blog/blogone/node_modules/express/lib/router/index.js:199:19)
at next (/media/bcd44b6a-9c2e-4496-81da-b45d3c349c91/mywork/nodejs/blog/blogone/node_modules/express/lib/router/index.js:176:38)
at /media/bcd44b6a-9c2e-4496-81da-b45d3c349c91/mywork/nodejs/blog/blogone/node_modules/express/lib/router/index.js:137:5
at /media/bcd44b6a-9c2e-4496-81da-b45d3c349c91/mywork/nodejs/blog/blogone/node_modules/express/lib/router/index.js:250:10
at next (/media/bcd44b6a-9c2e-4496-81da-b45d3c349c91/mywork/nodejs/blog/blogone/node_modules/express/lib/router/index.js:160:14)
at next_layer (/media/bcd44b6a-9c2e-4496-81da-b45d3c349c91/mywork/nodejs/blog/blogone/node_modules/express/lib/router/route.js:77:14)

我的路由器代码有什么问题..请帮帮我。我是 Node & Express 的新手

node.js 表达 node-mongoDB-原生

评论


答:

0赞 azero0 7/20/2014 #1

在:

db.collection('usercollection').remove({"_id":uid},function(err,result){
    ...
});

试试这个

评论

0赞 softboxkid 7/20/2014
我尝试了上面的代码。它返回了一个错误对象 #<Manager> 没有方法“集合”。
3赞 softboxkid 7/20/2014 #2

最后我解决了这个问题,这是我的代码:

    // Delete user
    router.get('/deleteuser/:id', function(req, res) { 

    var db = req.db;

    var uid = req.params.id.toString();
    var collection = db.get('usercollection');

    collection.remove({"_id":uid}, function(err, result) { 
        res.send( (result === 1) ? { msg: 'Deleted' } : { msg: 'error: '+ err } );
    });

});