I have created custom router path for save data in model (common/models/quotation.js). I want transaction also for this. So I've created transaction like this.
quotation.save = async(reqBody, include, cb) => {
try {
await app.dataSources.db.transaction(async models => {
const {quotation} = models;
const {guest} = models;
const rollBack = (location, err) => {
console.log(location);
throw err;
};
await quotation.create(quotationJSON, (quotationErr, quotationResponse) => {
if (quotationErr) {
rollBack('Quotation Save Err', quotationErr);
}
var guestArray = quotationJSON.guestArray;
guestArray.map(async(guestObj, guestIndex) => {
guestObj.guest_quotation_ID = quotationResponse.id;
if (quotationJSON.guestArray.length - 1 === guestIndex) {
await guest.create(guestArray, (guestErr, guestResponse) => {
if (guestErr) {
rollBack('Guest Save Err', guestErr);
}
console.log(guestResponse);
});
}
});
});
});
} catch (e) {
console.log(e); // Oops
}
};
when this run its give an error like this.
Error: The transaction is not active: 5e59b5f0-8b7a-11ea-a422-b1cd47789f87
so how can I solve this?