gpt4 book ai didi

javascript - 将多个对象从 Angular 保存到 Sequelize

转载 作者:行者123 更新时间:2023-12-03 22:41:34 24 4
gpt4 key购买 nike

我正在使用 mean.js 创建一个系统,我更改了 sequelize 的 Mongoose 部分,并尝试通过 sequelize 将多个对象从 Angular 保存到我的数据库中。

我按照这个 answer 在多个计划的 Dia(日)选项上动态创建多个输入。 enter image description here
我有这样的 Controller :

$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);
};

并查看
<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>

然后我在客户端使用 Angular 安装了两个 Controller :
// 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;
} );
};

并 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)
});
});

};

但老实说,我不知道如何保存它,或者我的 Angular Controller 是否是正确的方法。希望你能帮助我或给我提示如何去做。

最佳答案

除了更新单个实例,您还可以一次创建、更新和删除多个实例。您正在寻找的功能被称为

模型.bulk创建

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

关于javascript - 将多个对象从 Angular 保存到 Sequelize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42634019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com