简体   繁体   English

更新mongodb收集记录

[英]Update mongodb collection record

I'm playing around with node.js and mongo.db this weekend. 这个周末我正在玩node.js和mongo.db。 At the moment I'm trying to update a record within a collection. 目前,我正在尝试更新集合中的记录。

When the archiveuser function is called I'm getting a 'Failed to load resource: net::ERR_EMPTY_RESPONSE' in the developertools console. 调用archiveuser函数时,在developertools控制台中显示“无法加载资源:net :: ERR_EMPTY_RESPONSE”。 I'm using a similar snippet to the archiveuser function to delete users and this works perfectly. 我正在使用与archiveuser函数类似的代码段来删除用户,因此效果很好。 I can't understand why this is now failing. 我不明白为什么现在失败了。

I've written this snippet of code to perform the update within users.js: 我已经编写了这段代码以在users.js中执行更新:

router.put('/updateuser/:id', function(req, res) {
    var db = req.db;
    var userToUpdate = req.params.id;

    db.userlist.update({"_id" : userToUpdate}, 
        {
            $set:{'archived':'1'}
        })
});

Which is being called from global.js from this function: 通过以下函数从global.js调用:

function archiveUser(event) {
    $.ajax({
        type: 'UPDATE',
        url: '/users/updateuser/' + $(this).attr('rel')
    }).done(function( response ) {

        // Check for a successful (blank) response
        if (response.msg === '') {
            alert('Success');
        }
        else {
            alert('Error: ' + response.msg);
        }
    });
};

If I was using php with mysql I would run an update query, similar to this. 如果我在mysql上使用php,我将运行一个更新查询,类似于此。

UPDATE userlist
SET archived='1'
WHERE id=$usertodelete;

Any help is much appreciated 任何帮助深表感谢

Tony 托尼

As @Philipp pointed out, the code is fine and works as expected. 正如@Philipp指出的那样,该代码很好并且可以按预期工作。

The problem I was having is I didn't realise that the node server needed to be restarted with every change to a node.js js file (routes, app.js etc) 我遇到的问题是我没有意识到节点服务器需要对node.js js文件(路由,app.js等)进行每次更改都重新启动。

I have installed nodemon ( https://github.com/remy/nodemon ) which restarts the server whenever it detects a change. 我已经安装了nodemon( https://github.com/remy/nodemon ),它在检测到更改时会重新启动服务器。 This combined with live reloading the web browser has solved the problem. 结合实时重新加载Web浏览器已解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM