Mongoose: Find, modify, save

后端 未结 6 572
不思量自难忘°
不思量自难忘° 2020-11-28 06:49

I have a Mongoose User model:

var User = mongoose.model(\'Users\',
    mongoose.Schema({
        username: \'string\',
        password: \'strin         


        
6条回答
  •  生来不讨喜
    2020-11-28 07:15

    The user parameter of your callback is an array with find. Use findOne instead of find when querying for a single instance.

    User.findOne({username: oldUsername}, function (err, user) {
        user.username = newUser.username;
        user.password = newUser.password;
        user.rights = newUser.rights;
    
        user.save(function (err) {
            if(err) {
                console.error('ERROR!');
            }
        });
    });
    

提交回复
热议问题