检查用户名是否不在数据库中?

时间:2016-10-28 07:21:20

标签: javascript node.js mongodb express mongoose

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

  1. 如果我的用户想要更改为数据库中的现有用户名,它将使用现有用户名(而非用户当前用户名)进行更新
  2. 当请求的用户名未在数据库中存在时,它将不会更新,因为没有现有的用户名
  3. 这是我的代码,让所有其他字段工作,但用户名是一个......

    BigDecimal

    其次是

    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.');
          });
        });
    }
    

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

2 个答案:

答案 0 :(得分:0)

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

您需要再次重新考虑逻辑,并确定哪个字段将是您的唯一字段,并根据该字段重写所有字段。

答案 1 :(得分:0)

无论您拥有什么样的客户端(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