嗨,我在 sequilize 事务中遇到此错误,请谁能帮我解决这个问题,我正在 nodejs lambda 中执行此操作。
在输出此代码后,它也保存了数据,但它返回错误,指出"无法提交事务,因为它已完成状态:提交",下面是代码:
return database_object.sequelize.transaction({autocommit:false}, (transaction_object) => {
let update_attributes = {
date_updated : dt.format('Y-m-d H:M:S'),
is_returned : 1
};
console.info("UPDATE-create-return-attributes:info", update_attributes);
return database_object.order_item_details.model.update(update_attributes, {where : where_object, transaction : transaction_object})
.then(result => {
where_object = {
order_no : order_details.item_details.order_no,
website_id : order_details.item_details.website_id
};
return database_object.order_details.model.find({where : where_object, transaction : transaction_object})
.then(result => {
let customer_id = result.dataValues.customer_id;
let orderd_date = result.dataValues.order_date;
let return_create_obj;
if(!order_details.prev_return_request) {
return_create_obj = {
customer_id : customer_id,
subsidiary_id : order_details.allocation_data.subsidiary_id,
store_id : order_details.allocation_data.store_id,
order_id : order_details.item_details.order_id,
order_no : order_details.item_details.order_no,
item_id : item_details.item_id,
website_id : item_details.website_id,
lastmile_id : item_details.lastmile_id,
customerlastmilepartner_name : !item_details.customerlastmilepartner_name ? null : item_details.customerlastmilepartner_name,
sku : order_details.item_details.sku,
status : item_details.status,
lastmile_status : order_details.lastmile_allocation.status,
qty_to_return : item_details.qty_to_return,
reason_to_return : item_details.reason_to_return,
remarks : item_details.remark,
order_date : orderd_date,
request_type : item_details.request_type,
return_type : item_details.return_type,
type: item_details.type,
forward_awb_no: item_details.forward_awb_no,
reverse_awb_no: item_details.reverse_awb_no,
valid_until: item_details.valid_until,
is_manifested : order_details.lastmile_allocation.is_manifested,
create_by : item_details.create_by,
create_time : item_details.create_time,
modified_by : !item_details.modified_by ? item_details.create_by : item_details.modified_by,
modified_time : !item_details.modified_time ? item_details.create_time : item_details.modified_time,
mp_return_id : !item_details.mp_return_id ? null : item_details.mp_return_id
};
return database_object.item_return.model.create(return_create_obj, {transaction : transaction_object})
.then(result => {
transaction_object.commit();
return create_return_cb(null, {item_id : item_details.item_id, status : "Success", detail : "Return request created successfully for item"});
})
.catch(error => {
transaction_object.rollback();
console.error("UPDATE-create-return-attributes:error", error);
return create_return_cb(error);
});
} else {
update_attributes = {
qty_to_return : item_details.qty_to_return + order_details.prev_return_request.qty_to_return,
reason_to_return: item_details.remark,
request_type : item_details.request_type,
return_type : item_details.return_type,
type: item_details.type,
status : item_details.status,
forward_awb_no: item_details.forward_awb_no,
reverse_awb_no: item_details.reverse_awb_no,
valid_until: item_details.valid_until,
create_by: item_details.create_by,
create_time: item_details.create_time,
modified_time : dt.format('Y-m-d H:M:S'),//!item_details.modified_time ? item_details.create_time : item_details.modified_time,
lastmile_id : item_details.lastmile_id
};
where_object.customer_id = customer_id;
where_object.order_no = order_details.item_details.order_no;
console.info("UPDATE-create-return-attributes:info", update_attributes);
return database_object.item_return.model.update(update_attributes, {where : where_object, transaction : transaction_object})
.then(result => {
transaction_object.commit();
return create_return_cb(null, {item_id : item_details.item_id, status : "Success", detail : "Return request updated successfully for item"});
})
.catch(error => {
transaction_object.rollback();
console.error("UPDATE-create-return-attributes:error", error);
return create_return_cb(error);
});
}
})
.catch(error => {
transaction_object.rollback();
console.error("UPDATE-create-return-attributes:error", error);
return create_return_cb(error);
});
})
.catch(error => {
transaction_object.rollback();
console.error("UPDATE-create-return-attributes:error", error);
return create_return_cb(error);
});
});
我也可以看到如下事务日志。1. 开始交易;2. 设置自动提交 = 0;
在无法看到交易完成和完成的情况下。
使用非托管事务。 http://docs.sequelizejs.com/manual/tutorial/transactions.html#unmanaged-transaction-then-callback-
而不是sequelize.transaction({autocommit:false}, (transaction_object) => {});
使用sequelize.transaction({autocommit:false}).then((transaction_object) => {});
这意味着 sequelize 将根据承诺的成功或失败为您提交或回滚事务。我能够通过删除.commit()
呼叫来解决此问题。
删除{autocommit:false}
和所有其他手动提交和回滚代码(transaction_object.commit(), transaction_object.rollback()
(,使其成为纯托管事务。
对于非托管事务,请使用以下格式
const t = await sequelize.transaction();
try {
// Then, we do some calls passing this transaction as an option:
const user = await User.create({
firstName: 'Bart',
lastName: 'Simpson'
}, { transaction: t });
await user.addSibling({
firstName: 'Lisa',
lastName: 'Simpson'
}, { transaction: t });
// If the execution reaches this line, no errors were thrown.
// We commit the transaction.
await t.commit();
} catch (error) {
// If the execution reaches this line, an error was thrown.
// We rollback the transaction.
await t.rollback();
}