im workin with knex and node, when im trying to use transaction its not working and trhowing following error Unhandled rejection TypeError: container is not a function
, i dont know why what happend, anyone know whats wrong with my syntax or knowing better syntax?
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; } };
There were various errors in the code, something like this should work a bit better (this code too still looks horrible though):
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;
}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.