我与knex和节点一起工作,当我试图使用事务时,它不工作并出现以下错误
Unhandled rejection TypeError: container is not a function
,
我不知道为什么会发生什么,有人知道我的语法有什么问题或知道更好的语法吗?
const insertData = async (tableName, data) => {
try {
return knex(tableName)
.insert(data);
} catch (err) {
logger.error('error function insertData :global model', err);
return false;
}
};
const createAccount= async (dataAddress, dataAccount) => {
let trx = knex.transaction();
try {
const insertDataAddress = await insertData('mdm_address_contact', dataAddress);
await global.insertData('mdm_account1', Object.assign({ id_address: insertDataAddress[0] }, dataAccount));
trx.commit();
return true;
} catch (err) {
trx.rollback(err)
logger.error('error in function create account, mdm address contact:model', err);
return false;
}
};
答案 0 :(得分:1)
代码中存在各种错误,类似这样的代码应该会更好一些(尽管此代码看起来仍然很恐怖):
const insertData = async (tableName, data) => {
try {
return await knex(tableName).insert(data);
} catch (err) {
logger.error('error function insertData :global model', err);
throw err;
}
};
const createAccount = async (dataAddress, dataAccount) => {
try {
return await knex.tranasaction(async trx => {
const insertDataAddress = await insertData('mdm_address_contact', dataAddress);
await insertData('mdm_account1', Object.assign({ id_address: insertDataAddress[0] }, dataAccount));
});
} catch (err) {
logger.error('error in function create account, mdm address contact:model', err);
throw err;
}
};