2016-10-28 8 views
0

は、私は彼のプロフィールを更新し、私のユーザーを得ることができたが、私はこの問題に行きました:私のユーザーは、データベース内の既存のユーザー名に変更したい場合はユーザー名がデータベースにないかどうかを確認しますか?

  1. :私のユーザーは自分のユーザー名を変更したい場合、私は2つの問題に直面しています既存のユーザー名ここで

がないので、それは既存のユーザ名(ユーザの現在の)

  • が要求されたユーザ名は、データベースに文句を言わない更新をexisteしないで更新されますと、すべて取得するための私のコードです他のフィールドは動作していますが、ユーザ名は1つです...

    私はすでにネストされたリクエストを試してみましたが、それは単に動作しません

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

    続く

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

    ...: -/

  • 答えて

    0

    データベースのユーザー固有のキーとは何ですか?例えば、電子メールアドレスを持っているならば、それはユニークなものになり得ます。そのようにして、そのような問題は生じません。

    ロジックをもう一度考え直して、どのフィールドが独自のフィールドになるかを決め、それに従ってすべてを書き直す必要があります。

    +0

    私のユーザー名フィールドが既に一意である、あなたはすでに同じユーザー名でアカウントを作成することはできません! – antoine2vey

    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 
    
    関連する問題