2016-05-13 127 views
1

嗨,我自学了MEAN堆栈,并且有一个关于如何减少在我的代码中检查数量的问题。如何减少if语句的数量

基本上这个工作由用户填写他/她的设置页面,然后点击输入我们然后发送数据到服务器,所以我们可以更新mongo。

我似乎得到这个工作,让用户编辑某些领域,而不是所有的唯一方法是确保发送到服务器的数据不等于空,但肯定必须有一个更好的方法然后通过为每个字段运行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条语句。 –

+0

会将它添加到现在的队友 –

回答

0

您可以将所有属性添加到用户对象。

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

然后删除空值的键。

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

考虑使用Object.assign。它合并两个或多个对象,后者优先。以下假定data是一个对象,包含您的age,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 s不是问题。问题在于你向用户显示他们不应该看到的字段,所以问题出现在表示层中。

修复这一系列if s就像在地毯下扫除灰尘一样,您似乎希望为用户提供角色,因此请在您的代码中清楚明白并理解。

你能解决这个与服务器端生成HTML,像这样(语法可能是错误的,但我希望你明白了吧):

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

所以在你的HTML,你将有恰到好处的领域。

看看http://expressjs.com/en/guide/using-template-engines.html