Transaction that does insert and uses the id from that insert for next insert in transaction
Created by: jnystrom
WIthin a transaction, I am trying to insert an array of records, use the ids that were created from that batch insert to insert child records using the created id. I am getting the error below.
TypeError: Cannot read property 'promise' of undefined
message:"Cannot read property 'promise' of undefined"
stack:"TypeError: Cannot read property 'promise' of undefined\n at Writable.save (/Users/johnnystrom/dev/flock/pimms-ts/node_modules/massive/lib/writable.js:128:37)\n at SessionService.<anonymous> (/Users/johnnystrom/dev/flock/pimms-ts/lib/services/sessionService.js:60:50)\n at Generator.next (<anonymous>)\n at fulfilled (/Users/johnnystrom/dev/flock/pimms-ts/lib/services/sessionService.js:13:58)\n at process._tickCallback (internal/process/next_tick.js:68:7)"
__proto__:Error {constructor: , name: "TypeError", message: "", …}
Code:
db.withTransaction(async (tx: any) => {
try {
const savedRecs = await tx.localizations.insert(formattedLocalizations);
const networkIntegrations = await this.repo.getNetworkIntegrations(sessionId);
// no db actions done here
const integrationSubmissions = this.createIntegrationSubmissions(
sessionId,
savedRecs,
networkIntegrations
);
await tx.integration_submissions.insert(integrationSubmissions);
// update session to processedAt
await tx.sessions.save({ id: sessionId, processed_at: moment().utc() });
return;
} catch (error) {
this.log.error('Error adding recs:', error);
throw error;
}
});
The code above: the line that errors is the await tx.integration_submissions.insert(integrationSubmissions);
. That line is the one that is trying to insert w/ id created in the first tx call ( const savedRecs = await tx.localizations.insert(formattedLocalizations);
).If I remove that line it works.
Any advice would be greatly appreciated.
Thanks,.