NVO
NVO

Reputation: 2703

Angular, object property lost after assignment

I'm trying to set an new property to an object with Angular. I can see that the property is set with the right value, but when I want to save it with another function, the property is gone.

This is what I have so far:

$scope.confirmTask = function(taskId, taskString){
   campaignTaskCompleted(taskId, $scope.campaign._id).then(function(task){
      //Here completed == true
      console.log("task", task);
      $scope.saveSocialPortal();
   })
}

function campaignTaskCompleted(taskId, campaignId){
    return new Promise(function(resolve){
       for(var c =0; c < $scope.user.socialPortal.campaigns.length; c++){
          if($scope.user.socialPortal.campaigns[c]._id == campaignId){
             for(var i=0; i < $scope.user.socialPortal.campaigns[c].tasks.length; i++){
                if($scope.campaign.tasks[i]._id == taskId){
                   $scope.campaign.tasks[i].completed = true;
                   resolve($scope.campaign.tasks[i]);
                }
             }
          }
       }
   })
}

$scope.saveSocialPortal = function(){
   //here completed is completely gone
   console.log("save", $scope.user.socialPortal.campaigns[0].tasks);
   $api.put('user-social-portal', {socialPortal: $scope.user.socialPortal})
   .then(function(response) {

   })
   .catch(function(reason) {
       console.log(reason);
   })
}

What is the reason for this? What can I do to solve this?

Edit:

I was confusing different variables ($scope.campaign and $scope.user.socialPortal.campaigns) so some things are going wrong from the beginning on this page. I completely rewrote the code to always use $scope.user.socialPortal, and that is working.

Upvotes: 1

Views: 311

Answers (1)

jonahe
jonahe

Reputation: 5000

Maybe I'm reading your code wrong. But it seems you set the new completed property to true only on $scope.campaign.tasks[i] not on the $scope.user.socialPortal.campaigns[0]. Maybe they should be pointing to the same object? It's not clear if they do.

Wouldn't you at least need to somehow push the completed campaign into the $scope.user.socialPortal.campaigns array?

Upvotes: 1

Related Questions