Yuki Takei 8 лет назад
Родитель
Сommit
ed71e6b053
2 измененных файлов с 6 добавлено и 14 удалено
  1. 0 10
      lib/models/user.js
  2. 6 4
      lib/routes/me.js

+ 0 - 10
lib/models/user.js

@@ -131,16 +131,6 @@ module.exports = function(crowi) {
     return false;
     return false;
   };
   };
 
 
-  userSchema.methods.update = function(name, email, lang, callback) {
-    this.name = name;
-    this.email = email;
-    this.lang = lang;
-
-    this.save(function(err, userData) {
-      return callback(err, userData);
-    });
-  };
-
   userSchema.methods.updateLastLoginAt = function(lastLoginAt, callback) {
   userSchema.methods.updateLastLoginAt = function(lastLoginAt, callback) {
     this.lastLoginAt = lastLoginAt;
     this.lastLoginAt = lastLoginAt;
     this.save(function(err, userData) {
     this.save(function(err, userData) {

+ 6 - 4
lib/routes/me.js

@@ -89,20 +89,22 @@ module.exports = function(crowi, app) {
         return res.render('me/index', {});
         return res.render('me/index', {});
       }
       }
 
 
-      User.findOne({email: email}, (err, existingUserData) => {
-        userData.update(name, email, lang, (err, userData) => {
+      User.findOneAndUpdate(
+        { email: userData.email },                  // query
+        { name, email, lang },                      // updating data
+        { runValidators: true, context: 'query' },  // for validation
+                                                    //   see https://www.npmjs.com/package/mongoose-unique-validator#find--updates -- 2017.09.24 Yuki Takei
+        (err) => {
           if (err) {
           if (err) {
             Object.keys(err.errors).forEach((e) => {
             Object.keys(err.errors).forEach((e) => {
               req.form.errors.push(err.errors[e].message);
               req.form.errors.push(err.errors[e].message);
             });
             });
             return res.render('me/index', {});
             return res.render('me/index', {});
           }
           }
-
           req.i18n.changeLanguage(lang);
           req.i18n.changeLanguage(lang);
           req.flash('successMessage', req.t('Updated'));
           req.flash('successMessage', req.t('Updated'));
           return res.redirect('/me');
           return res.redirect('/me');
         });
         });
-      });
 
 
     } else { // method GET
     } else { // method GET
       /// そのうちこのコードはいらなくなるはず
       /// そのうちこのコードはいらなくなるはず