2016-05-13 8 views
1

こんにちは私は自分自身を覚えているスタックと私のコードでチェックの場合の量を減らす方法に関する質問があります。ifステートメントの量を減らす方法

基本的にこれはユーザーが設定ページを入力してから入力してからサーバーにデータを送信するので、mongoを更新できます。

私はこれを利用して特定のフィールドを編集できるようにしているようですが、すべてがサーバーに送信されるデータを確実にnullにはしませんが、確実に良い方法が必要ですすべてのフィールドに対してifステートメントを実行します。

問題のコードは、この

 //user.username = req.body.username; 

    if (age != null) { 

     user.age = age; 

    } 
    if (bio != null) { 

     user.bio = bio; 

    } 

    if (location != null) { 

     user.location = location; 

    } 

    if (team != null) { 

     user.team = team; 

    } 

    if (tags != null) { 

     user.tags = tags; 

    } 

    if (email != null) { 

     user.email = email; 

    } 

クライアント側コード

$scope.savesettings = function(provider){ 
    var theUser = JSON.parse(localStorage.getItem("User-Data")); 
    var user = theUser["_id"]; 
    var request = {}; 
    var request = { 

     user: user, 
     username: $scope.settings_username, 
     email: $scope.settings_email, 
     age: $scope.settings_age, 
     location: $scope.settings_location, 
     team: $scope.settings_team, 
     bio:$scope.settings_bio, 
     profilebanner: $scope.settings_profilebanner, 
     avatar: $scope.settings_avatar 

    }; 

    console.log(request); 

    //send to server 
    $http.put('api/social/updatesettings', request).success(function(response){ 

     alertify.success("Your settings have been successfully saved."); 

      localStorage.clear(); 
      localStorage.setItem('User-Data', JSON.stringify(response)); 



    }).error(function(error){ 

     alertify.error("Hmmm an issue has occured."); 

    }); 


}; 

サーバコード

var User = require('../../datasets/userModel'); 

module.exports.updatesettings =関数(REQ、RES){

あります
var age = req.body.age; 
    var bio = req.body.bio; 
    var location = req.body.location; 
    var team = req.body.team; 
    var tags = req.body.tags; 
    var email = req.body.email; 
    var profilebanner = req.body.profilebanner; 
    var avatar = req.body.avatar; 

User.findOne({_id: req.body.user}, function (err, user){ 


    //user.username = req.body.username; 

    if (age != null) { 

     user.age = age; 

    } 
    if (bio != null) { 

     user.bio = bio; 

    } 

    if (location != null) { 

     user.location = location; 

    } 

    if (team != null) { 

     user.team = team; 

    } 

    if (tags != null) { 

     user.tags = tags; 

    } 

    if (email != null) { 

     user.email = email; 

    } 

    user.save(function(err){ 

     if (err){ 
      console.log(err); 
      res.status(500).send(); 
      //res.json(user); 
     } else { 
      console.log("success"); 
      res.json(user); 
     } 
    }) 
}); 

};

+0

どこから変数を割り当てていますか?たとえば、 'if(age!= null)'、 'age'はどこから来ますか? – chridam

+0

配列内のすべてのフィールドをラップし、それに対してforEach()メソッドを実行すると、1つのif文しか持たないためコードが少なくなります。 –

+0

今すぐ仲間に追加します –

答えて

0

ユーザーオブジェクトにすべての属性を追加できます。

var user = { 
    age: age, 
    bio: bio, 
    location: location 
} 

次に、NULL値のキーを削除します。

for (var key in user) { 
    if (user[key] === null) { 
     delete user[key]; 
    } 
} 
2

Object.assignを使用することを検討してください。 2つ以上のオブジェクトをマージし、後者のオブジェクトを優先します。以下は、dataage,bioなどで、userが含まれているオブジェクトであるとします。あなたのuserオブジェクトです。

var results = Object.assign({}, user, data); 

は、このためにpolyfillsがあり、そしてあなたはjQueryのを使用するために起こる場合は、$.extendはほとんど同じ仕事をしていません。

0

変数の配列を渡す関数はどのようにしてsomeを使用して、いずれかがnullであるかどうかを確認します。

function isComplete(args) { 
    return !args.some(function(el) { 
    return el === null; 
    }); 
} 

var age = null; 
var bio = 'Something'; 
var location = null; 

isComplete([age, bio, location]); // false 
0

ifでは問題ありません。問題は、表示されるはずのないフィールドをユーザーに表示していることです。そのため、問題はプレゼンテーションレイヤーにあります。

そのシリーズのifを修正することは、カーペットの下のすっきりしたほこりに似ているため、ユーザーの役割を欲しがっているように思われるので、コードで明確に理解できるようにしてください。

<% if (user.canSetTeam()) { %> 
    <input type="text" name="team" /> 
<% } %> 

だからあなたのHTMLにあなただけの右のフィールドがあります:

あなたは、サーバー側でこれを修正することができはHTML、この(構文が間違っているかもしれないが、私はあなたがポイントを得る願っています)のようなものが生成されます。

見てくださいhttp://expressjs.com/en/guide/using-template-engines.html

関連する問題