如何在Mongoose中更新/上传文档/数据? Passport MongoDB,Express,AngularJS,Nodejs

时间:2018-07-20 17:29:02

标签: mongodb express mongoose passport.js upsert

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 个答案:

答案 0 :(得分:0)

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


}

User.update( {_id:req.user._id}, 新用户, {upsert:true}, 功能(错误,用户, 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);      

返回完成(为null,newUser,req.flash(“ passMessage”,“密码已成功更改”。));

            }});

});

}));