【问题标题】:Check if username is not in database?检查用户名是否不在数据库中?
【发布时间】:2016-10-29 04:25:52
【问题描述】:

我设法让我的用户更新了他的个人资料,但我遇到了一个问题:当我的用户想要更改他的用户名时,我面临两个问题:

  1. 如果我的用户想要更改为数据库中现有的用户名,它将更新现有的用户名(不是用户的当前用户名)
  2. 当请求的用户名在数据库中不存在时,它不会更新,因为没有现有的用户名

这是我的代码,可让所有其他字段正常工作,但用户名的字段...

exports.update = function(req,res){

  req.checkBody('username', 'Email is required').notEmpty().isEmail();
  req.checkBody('password', 'Password is required').notEmpty();
  req.checkBody('name', 'Name is required').notEmpty();
  req.checkBody('surname', 'Surname is required').notEmpty();
  req.checkBody('codePostal', 'Code postal is required').notEmpty();
  req.checkBody('phoneNumber', 'Téléphone is required').notEmpty();
  req.checkBody('address', 'Address is required').notEmpty();
  req.checkBody('town', 'Town is required').notEmpty();

  var errors = req.validationErrors();
  console.log(errors);
  if (errors) {
    res.status(400).send(errors);
    return;
  }

  var salt = bcrypt.genSaltSync(10),
  hash = bcrypt.hashSync(req.body.password, salt);

  User.findOne({ username: req.body.username }, function(err, user) {
      if (err) {
        return res.status(400).send('Error updating account.');
      }

      user.username = req.body.username;
      user.password = hash;
      user.name = req.body.name;
      user.surname = req.body.surname;
      user.codePostal = req.body.codePostal;
      user.phoneNumber = req.body.phoneNumber;
      user.address = req.body.address;
      user.town = req.body.town

      user.save(function(err) {
        if (err) {
          console.log(err);
          res.status(500).send('Error updating account.');
          return;
        }
        res.status(200).send('Account updated.');
      });
    });
}

后跟一个

userRoute = require('./server/api/user/index');
app.post('/account/update', authorizeRequest, userRoute.update);

function authorizeRequest(req, res, next) {
  if (req.isAuthenticated()) {
    next();
  } else {
    res.status(401).send('Unauthorized. Please login.');
  }
}

我已经尝试过嵌套请求,但它根本不起作用... :-/

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    您的数据库用户唯一键是什么?例如,如果您有电子邮件地址,它可以是唯一的,这样您就不会遇到这样的问题。

    你需要重新思考你的逻辑并决定哪个字段将是你的唯一字段,并据此重写所有内容。

    【讨论】:

    • 我的用户名字段已经是唯一的,你不能用相同的用户名创建帐户!
    【解决方案2】:

    无论您拥有什么客户端(Android、iOS、Angular),保留一个隐藏字段,其中包含用户的现有username 和另一个具有相同值但他可以编辑的字段。以不同的方式命名它们。

    在后端,找到原始并更新为新的。

      req.checkBody('username', 'Email is required').notEmpty().isEmail(); // original/current
      req.checkBody('newUsername', 'Email is required').notEmpty().isEmail(); //new
      req.checkBody('password', 'Password is required').notEmpty();
      req.checkBody('name', 'Name is required').notEmpty();
      req.checkBody('surname', 'Surname is required').notEmpty();
      req.checkBody('codePostal', 'Code postal is required').notEmpty();
      req.checkBody('phoneNumber', 'Téléphone is required').notEmpty();
      req.checkBody('address', 'Address is required').notEmpty();
      req.checkBody('town', 'Town is required').notEmpty();
    

    User.findOne({ username: req.body.username }, function(err, user) {
          if (err) {
            return res.status(400).send('Error updating account.');
          }
    
          user.username = req.body.newUsername; //pass new
    

    【讨论】:

      猜你喜欢
      • 2021-03-20
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多