0

I'm using mean.js to create a system and I change the mongoose part for sequelize and I trying to save multiple Objects from Angular to my database through sequelize.

I followed this answer to create multiple inputs dynamically on the Dia (Day) option for multiple schedules.enter image description here And I have my controller like this:

$scope.horarios = [];

$scope.itemsToAdd = [{
  Day: '',
  StartHour: '',
  EndHour: ''
}];

$scope.add = function(itemToAdd) {

  var index = $scope.itemsToAdd.indexOf(itemToAdd);

  $scope.itemsToAdd.splice(index, 1);

  $scope.horarios.push(angular.copy(itemToAdd))
};

$scope.addNew = function() {

  $scope.itemsToAdd.push({
    Day: '',
    StartHour: '',
    EndHour: ''
  });
  console.log($scope.itemsToAdd);
};

and view

<div class="col-xs-12" style="padding: 0" ng-repeat="itemToAdd in itemsToAdd">
  <div class="form-group col-xs-12 col-sm-5" >
    <label for="Days">Dia</label> <select class="form-control col-xs-12 col-sm-6" data-ng-model="itemToAdd.Day" id="Days" name="Days">
      <option value="">---Seleccione uno---</option>
     ....
    </select>
  </div>
  <div class="form-group col-xs-5 col-sm-3">
    <label class="control-label" for="startHour">Hora Inicio</label> <input class="form-control" id="startHour" name="startHour" ng-model="itemToAdd.StartHour" type="time">
  </div>
  <div class="form-group col-xs-5 col-sm-3">
    <label class="control-label" for="endHour">Hora Termino</label> <input class="form-control" id="endHour" name="endHour" ng-model="itemToAdd.EndHour" type="time">
  </div>
  <div class="col-xs-2 col-sm-1">
    <button ng-click="addNew()" class="btn btn-success" style="position: relative; top:26px"><i class="glyphicon glyphicon-plus"></i></button>
  </div>
</div>

Then I have my both controllers on client side with Angular:

// Create new course
$scope.create = function( isValid ) {
  // Create new course object
  var course = new Courses( $scope.course );
  course.Schedule = $scope.itemsToAdd;

  console.log($scope.course);
  // Redirect after save
  course.$save( function( response ) {
    $scope.closeThisDialog();
    notify( 'Genial. El Curso ha sido registrada exitosamente' );
    // Clear form fields
    $scope.course = '';
    $scope.schedule = '';
  }, function( errorResponse ) {
    $scope.error = errorResponse.data.message;
  } );
};

And sequelize:

exports.create = function(req, res) {
var schedule = req.body.Schedule;
req.body.schedule = undefined;
// req.body.userId = req.user.id;
db.Course.create(req.body)
    .then(function(course) {
        if (!course) {
            return res.send('users/signup', {
                errors: 'Could not create the course'
            });
        } else {
            schedule.CourseId = course.dataValues.id;

            db.Schedule.create(schedule)
                .then(function(schedule) {
                    for (var i = schedule.dataValues.length - 1; i >= 0; i++) {
                        course.schedule = schedule.dataValues[i];
                    }
                    // course.schedule = schedule.dataValues;
                })
                .catch(function(err) {
                    return res.status(400).send({
                        message: errorHandler.getErrorMessage(err)
                    });
                });

            return res.jsonp(course);
        }
    })
    .catch(function(err) {
        return res.status(400)
            .send({
                message: errorHandler.getErrorMessage(err)
            });
    });

};

But honestly I don't have a clue how to save it or if my Angular controller is even the correct way to do it. Hope you can help me or give me hint how to do it.

Community
  • 1
  • 1
Ellebkey
  • 2,201
  • 3
  • 22
  • 31
  • You need to use the method bulkCreate: Look for it here: http://docs.sequelizejs.com/en/latest/docs/instances/ . I am not sure though, if this method returns the created records. – yBrodsky Mar 06 '17 at 19:49

1 Answers1

0

In addition to updating a single instance, you can also create, update, and delete multiple instances at once. The functions you are looking for are called

Model.bulkCreat

http://docs.sequelizejs.com/en/2.0/docs/instances/#working-in-bulk-creating-updating-and-destroying-multiple-rows-at-once

  • The last comment also told me about bulk creation, but how do I use it when I have a changeful $scope. The schedules could be from one day to 6 , Monday to Saturday. What I did was create a bucle and call the save function for schedule for every item inside the schedule $scope. `angular.forEach($scope.Schedules, function(row, index){ var horario = new Schedules(row); horario.$save(); });` But I don't think that's the best way to do it. – Ellebkey Mar 14 '17 at 16:46