如何删除mongodb + nodejs + expressjs的具体数据

时间:2014-07-20 04:14:38

标签: node.js express node-mongodb-native

如何使用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)

我的路由器代码出了什么问题..请帮帮我。我是节点和节目的新手表达

2 个答案:

答案 0 :(得分:1)

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

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

});

答案 1 :(得分:0)

中的:

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

试试这个