mongoose操作mongodb报错 _id拒绝
发布于 11 年前 作者 feiquan 4198 次浏览 最后一次编辑是 8 年前

使用nodejs+mongoose更新用户账号时报_id主键拒绝 enter image description here

exports.updateAccount = function(req, res, next) {
var proxy = new EventProxy();

var render = function(data) {
    res.send(data);
};

proxy.assign('updateAccount', render);

var where = {};

var userId = req.param('userid')
    , account = req.param('account')
    , password = req.param('password2')
    , username = req.param('username');

where = {'_id': userId, 'password': password};
Account.findOne(where, function(err, account) {
    if (err)
        return next(err);

    var result;
    if (account != null) {

        account.account = account;
        account.password = password;
        account.username = username;
        console.log(account+'dd');
        account.update(function(err) {
            if (err)
                return next(err);
        });
        result = {'success': true};
    } else {
        result = {'success': false};
    }
    proxy.trigger('updateAccount', result);
});

}

5 回复

说明下你的代码很严重的问题,你在使用account.accout = accout的时候就没有意识到accout对应的是哪个__account__吗?你这样会出现对象链的循环递归引用嵌套!

后来调试知道了这个问题 但是不让更新还是存在 纠结了好久了

好像 update是类对象的方法吧 实例对象用save方法

我调试下看能行不 呵呵

确实实例使用save方法

回到顶部