如何在 Mongoose 中更新/更新文档/数据? Passport MongoDB、Express、AngularJS、Nodejs
Posted
技术标签:
【中文标题】如何在 Mongoose 中更新/更新文档/数据? Passport MongoDB、Express、AngularJS、Nodejs【英文标题】:How do I update/upsert a document/Data in Mongoose? Passport MongoDB, Express, AngularJS, Nodejs 【发布时间】:2018-12-29 01:37:44 【问题描述】:var LocalStrategy = require('passport-local').Strategy;
// load up the user model
var User = require('../app/models/user');
// save the user
newUser.save(function(err)
if (err)
throw err;
return done(null, newUser);
);
【问题讨论】:
【参考方案1】:passport.use('local-update', new LocalStrategy(
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true //allowsustopassbacktheentirerequest to the
,
function(req, email, password, done) // callback with email and password
console.log("local-update")
// find a user whose email is the same as the forms email
console.log(req.user._id)
//console.log(email)
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
// if there is no user with that email
// create the user
var newUser = new User();
// set the user's local credentials
newUser.local.username = req.user.local.username;
newUser.local.email = email;
newUser.local.password = newUser.generateHash(password);
console.log(password)
console.log(newUser)
//console.log(newUser)
// update the user
newUser._id=req.user._id;
//如果找到用户但密码错误
User.findOne( _id: req.user._id,
函数(错误,用户)
// if there are any errors, return the error before anything else
if (err)
return done(err);
否则
// all is well, return successful user
console.log(password);
用户更新( _id: req.user._id, 新用户, 更新:真, 功能(错误,用户, numberAffected, rawResponse)
if (err)
console.log('new profile update error');
return done(err);
// all is well, return successful user
else
// if (User.setUser(newUser.local.password))
console.log('new profile update');
console.log(user);
return done(null, newUser,req.flash('passMessage', '密码更改成功。'));
);
);
));
【讨论】:
以上是关于如何在 Mongoose 中更新/更新文档/数据? Passport MongoDB、Express、AngularJS、Nodejs的主要内容,如果未能解决你的问题,请参考以下文章