Как правильно вызвать сохранение в цикле?
Проблема в асинхронных вызовах. Нужно как то вызывать функцию сохранения в цикле после того как пришёл ответ от сервера. Или проблема в чём то другом
service.prototype.save = function (menu_id, callback) {
var that = this;
angular.forEach(this.permissions, function (value, key) {
if (value) {
that.newItem.menu_id = menu_id;
that.newItem.permission_id = key;
console.log(that.newItem);
that.resource.save({action: that.action}, that.newItem, function (data) {
console.log(typeof data);
console.log(data);
// that.items.push(data);
that.newItem = {
action: that.action
};
that.permissions = [];
if (typeof callback == 'function') {
callback(data);
}
});
}
});
};
Вот так работает:
service.prototype.save = function (menu_id, callback) {
var that = this;
angular.forEach(this.permissions, function (value, key) {
if (value) {
$timeout(function () {
that.newItem.menu_id = menu_id;
that.newItem.permission_id = key;
console.log(that.newItem);
that.resource.save({action: that.action}, that.newItem, function (data) {
console.log(typeof data);
console.log(data);
// that.items.push(data);
that.newItem = {
action: that.action
};
that.permissions = [];
if (typeof callback == 'function') {
callback(data);
}
});
}, 1000 * key);
}
});
};
Ошибка:
data: "
↵Notice: Undefined index: error in C:\server\OpenServer\domains\REST\resource\index.php on line 116↵{"msg":[],"post":{"action":"MenuPerm","menu_id":"97","permission_id":"2"},"error":"PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '97-2' for key 'test2'\n File=C:\server\OpenServer\domains\REST\resource\index.php\n Line=105\n#0 [internal function]: PDOStatement->execute()\n#1 [internal function]: Phalcon\Db\Adapter\Pdo->executePrepared(Object(PDOStatement), Array, Array)\n#2 [internal function]: Phalcon\Db\Adapter\Pdo->execute('INSERT INTO `me...', Array, Array)\n#3 [internal function]: Phalcon\Db\Adapter->insert('menu_perm', Array, Array, Array)\n#4 [internal function]: Phalcon\Mvc\Model->_doLowInsert(Object(Phalcon\Mvc\Model\MetaData\Memory), Object(Phalcon\Db\Adapter\Pdo\Mysql), 'menu_perm', 'id')\n#5 C:\server\OpenServer\domains\REST\resource\index.php(105): Phalcon\Mvc\Model->save()\n#6 [internal function]: {closure}('MenuPerm')\n#7 C:\server\OpenServer\domains\REST\resource\index.php(204): Phalcon\Mvc\Micro->handle()\n#8 {main}"}"
CREATE TABLE likemyjob.menu_perm (
id int(11) NOT NULL AUTO_INCREMENT,
menu_id int(11) DEFAULT NULL,
permission_id int(11) DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE INDEX test2 (menu_id, permission_id),
CONSTRAINT FK_menu_perm_menu_id FOREIGN KEY (menu_id)
REFERENCES likemyjob.menu (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FK_menu_perm_permission_id FOREIGN KEY (permission_id)
REFERENCES likemyjob.permission (id) ON DELETE CASCADE ON UPDATE CASCADE
)
ENGINE = INNODB
AUTO_INCREMENT = 105
AVG_ROW_LENGTH = 682
CHARACTER SET utf8
COLLATE utf8_general_ci;
Попробовал сделать так результат тот же:
service.prototype.save = function (menu_id, callback) {
var that = this;
angular.forEach(this.permissions, function (value, key) {
if (value) {
that.newItem.menu_id = menu_id;
that.newItem.permission_id = key;
that.helpSave(that.newItem);
}
});
};
service.prototype.helpSave = function (newItem) {
console.log(newItem);
var url = "/resource/source/MenuPerm";
$http.post(url, newItem).
then(function (response) {
console.log(response);
}.bind(this), function (response) {
console.log(response);
});
};