1

I have a functioning code which creates a new JSON object based on my schema but I'd like to target the objects inside of the overall object. I'm not sure if this is possible... I'm stuck in between creating a new object but also updating an existing one

Here is my schema

year: { 
January: [
  {
    day: String,
    title: String,
    summary: String,
    description: String
  }
      ],

December: [
  {
    day: String,
    title: String,
    summary: String,
    description: String
  }
      ]
}
});

Here is my data in mongodb

{
  _id: "53ee9f0fc6aed109c6d33cfc"
  __v: 0
  -year: {
    December: [ ]
    November: [ ]
    October: [ ]
    September: [ ]
    August: [ ]
    July: [ ]
    -June: [
     -{
       day: "21"
       title: "ok"
       summary: "ok"
       description: "ok"
       _id: "53ee9f0fc6aed109c6d33cfd"
       }
         ]
    May: [ ]
    April: [ ]
    March: [ ]
    February: [ ]
    January: [ ]
    }
}

My problem is my logic as it currently is creates a whole new 'year' JSON object, where I would like to work with _id: "53ee9f0fc6aed109c6d33cfc" and add dates to each month.

Here is my current angular logic:

$scope.createEvent = function() {
  var cal = new CAL.API();


 var month = [{day:$scope.calDay, title: $scope.calTitle, summary: $scope.calSummary, 
        description: 'ok'}];
              cal.year = {};
              cal.year[$scope.calMonth] = month;
              cal.$save(function(result){
                $scope.calendar.push(result);
              });
            } 
PrairieProf
  • 174
  • 11
byrdr
  • 5,197
  • 12
  • 48
  • 78
  • possible duplicate of [Using angular foreach loop to rearrange JSON](http://stackoverflow.com/questions/25390532/using-angular-foreach-loop-to-rearrange-json) – falsarella Mar 21 '15 at 22:23

1 Answers1

0

Are you trying to push calendar events to a calendar object, right?

If yes, you need to retrieve the actual calendar and then push event to the specific day instead of create new calendars for each event because in this case you does not save a single calendar with full events.

The other option is save a object for each event and query all events that match with the month and days that you want

For the first option:

var cal = getMyActualCal();

and then check if the Month and Day exists and create if not.

cal.year[$scope.calMonth] = cal.year[$scope.calMonth] || []

var event = { day: $scope.calDay, title: 'mytitle', sumary: 'mysumary' };

cal.year[$scope.calMonth].push(event);

cal.$save();
CarlosCondor
  • 121
  • 1
  • 4