将代码重构为momentjs的一个函数

时间:2018-05-08 23:46:56

标签: angularjs angularjs-scope refactoring momentjs angular-moment

我有2个函数prev()和next(),它们的代码几乎相同。

有没有办法可以将2个功能合并为一个功能?

    const mongoose = require('mongoose'
    const mysql = require('mysql');

    // Create mySQL connection
    const mySQLdb = mysql.createConnection({
      host     : 'localhost',
      user     : 'root',
      password : 'root',
      database : 'projects'
    });

    const Project = mongoose.model('project');

    router.post('/developerSave', async (req, res) => {
      let devList = req.body.dev_ids,
      devIdArr = devList.split(','),
      rData = {};

    // get project
    const project = await Project.findById(req.body.projectID);

    mySQLdb.connect();

    for(var i=0, len=devIdArr.length; i < len; i++) {      
      let sql = `SELECT CONCAT(first_name, ' ', last_name) as full_name FROM users WHERE id= '${devIdArr[i]}'`;

      mySQLdb.query(sql, function (err, results) {
        if (err) throw err;    

    let newDev = {
          userId: devIdArr[i],
          fullName: results[0].full_name
        }

        project.developers.unshift(newDev);
        await project.save();

      });

    }

    mySQLdb.end();

    rData.success = true;
    rData.msg = 'Developer was added successfully.';

    res.status(200).json(rData);
    });

我尝试了以下但不起作用

<button id="prev-search-button" ng-click="prev()"><</button>  
<button id="next-search-button" ng-click="next()">></button> 

$scope.prev = function(){ 
  var search = $scope.inputDate ? $scope.inputDate: moment() ;  
  $scope.day = moment(search).subtract(1, 'days').format('DD'); 
  $scope.month=moment(search).subtract(1, 'days').format('MM'); 
};

$scope.next = function(){
  var search = $scope.inputDate ? $scope.inputDate: moment();  
  $scope.day = moment(search).add(1, 'days').format('DD');
  $scope.month = moment(search).add(1, 'days').format('MM'); 
};

1 个答案:

答案 0 :(得分:1)

首先: ng-click="prevAdd(subtract)"ng-click="prevAdd(add)"错误,将参数作为字符串或整数传递,如ng-click="prevAdd('subtract')"ng-click="prevAdd('add')"

第二: arg中没有方法moment(search).arg,它只有addsubtract。所以以下陈述是错误的:

$scope.day = moment(search).arg(1, 'days').format('DD'); 
$scope.month=moment(search).arg(1, 'days').format('MM');

应该写成:

  if(arg === 'add') {
     $scope.day = moment(search).add(1, 'days').format('DD'); 
     $scope.month=moment(search).add(1, 'days').format('MM');
  } 
  if(arg === 'subtract') {
     $scope.day = moment(search).subtract(1, 'days').format('DD'); 
     $scope.month=moment(search).subtract(1, 'days').format('MM');
  }  
  

请使用以下最终修改代码:

<button id="prev-search-button" ng-click="prevAdd('subtract')"><</button> 
<button id="prev-search-button" ng-click="prevAdd('add')"><</button>  

$scope.prevAdd = function(arg){  
  var search = $scope.inputDate ? $scope.inputDate: moment() ; 
  if(arg === 'add') {
     $scope.day = moment(search).add(1, 'days').format('DD'); 
     $scope.month=moment(search).add(1, 'days').format('MM');
  } 
  if(arg === 'subtract') {
     $scope.day = moment(search).subtract(1, 'days').format('DD'); 
     $scope.month=moment(search).subtract(1, 'days').format('MM');
  }  
};