$ http.put没有更新模型

时间:2016-10-02 19:14:20

标签: javascript angularjs mean-stack mean

我正在尝试学习MEAN堆栈并学习使用$ http服务。

我目前正在进行全局检查,以便更新我的Sprints模型,如下所示:

var SprintSchema = new Schema({
  tasks: [{
    type: String,
    ref: 'Task'
  }],
  name: {
    type: String
  },
  start: {
    type: Date
  },
  end: {
    type: Date
  },
  active: Boolean
});

以下控制器应该在请求时更新Sprint模型,并且当我在成功函数中调试变量时,它看起来像我期望它通过但它实际上并没有最终更新我的模型。下面是我的代码和console.log的一个例子。

'use strict';

angular.module('inBucktApp')
  .service('VariableService', function () {
    // AngularJS will instantiate a singleton by calling "new" on this function
        var ticketId = 'noTicketYet';
        var ticketAssigneeName  = 'noTicketNameYet';

        return {
            getPropertyId: function () {
                return ticketId;

            },
            getPropertyName: function () {
                return ticketAssigneeName;

            }
            ,
            setProperty: function(value, valueName) {
                ticketId = value;
                ticketAssigneeName  = valueName;
            }
        };
      })
    .run(['$rootScope', '$http', 'socket', 'VariableService', function($rootScope, $http, socket, VariableService) {

        $rootScope.sprintStart;

        $http.get('/api/sprints').success(function(sprints) {

          $rootScope.sprints = sprints.pop();

          $rootScope.sprintStart = new Date($rootScope.sprints.start);
          $rootScope.sprintEnd = new Date($rootScope.sprints.end);

          socket.syncUpdates('sprints', $rootScope.sprints);


          $http.get('/api/tasks').success(function(task) {
            $rootScope.task = task;
            $rootScope.taskPop = _.flatten($rootScope.task);
            $rootScope.taskPopAgain = $rootScope.task.pop();

            socket.syncUpdates('task', $rootScope.task);
            
        $rootScope.updateTicket = function(){


        //Goes through the entire array and check each element based on critera.
        var taskIdsToAdd = [];

        for(var i = 0; i < $rootScope.taskPop.length; i++){
          var taskFind = $rootScope.taskPop[i];
          //Logic if ticket is not in the sprint
          if ((new Date(taskFind.start) >= $rootScope.sprintStart)  && (new Date(taskFind.start) <= $rootScope.sprintEnd)){
            taskFind.sprint = true;

            taskIdsToAdd.push(taskFind._id);
            $rootScope.sprints.tasks.push(taskFind._id);

           $http.put("/api/tasks/"+taskFind._id,taskFind).success(function(task){
             console.log('Logic 1 Ran!');
             console.log($rootScope.sprintStart);
            //  socket.syncUpdates('taskPopAgain', taskFindPopAgain);
           });
           $http.put("/api/sprints/"+$rootScope.sprints._id,$rootScope.sprints).success(function(sprints){
             console.log('Logic 2 Ran!');
             console.log($rootScope.sprintStart);
             console.log(sprints)
           });
           console.log($rootScope.sprints);
         } else{
           console.log('this doesnt work first');
         };


         //Logic if ticket is not in the sprint
         if (new Date(taskFind.start) < $rootScope.sprintStart  ||  new Date(taskFind.start) > $rootScope.sprintEnd){
           taskFind.sprint = false;
           $http.put("/api/tasks/"+taskFind._id,taskFind).success(function(task){
             console.log(task);
           });

         }else{
           console.log('this doesnt work');
         };
        }
        };
        $rootScope.updateTicket();
        });
        });
    }]);

console.log(sprints)的Console.Log

Console.Log of console.log(sprints)

任何人都知道我在做什么不正确吗?

感谢帮助人员。

0 个答案:

没有答案