Transactions Failing in Lambda
See original GitHub issueSummary:
I’m using Dynamoose in Node lambdas with version 10.x. I get this error when trying to use transactions.
It works on my local machine with serverless offline but not in a lambda.
TypeError: Cannot read property ‘transactWriteItems’ of undefined at transact (/var/task/node_modules/dynamoose/dist/Dynamoose.js:226:26) at Dynamoose.transaction (/var/task/node_modules/dynamoose/dist/Dynamoose.js:262:13) at process._tickCallback (internal/process/next_tick.js:68:7)
Code sample:
Schema
// Code here
// Showing how transactions are used, para-phrased.
const role = Role.transaction.create({
id: roleId,
tenantId: tenantId,
owner: body.userId,
});
// Save Tenant
const result = await dynamoose.transaction([tenant, role, ...locations]);
Current output and behavior:
TypeError: Cannot read property ‘transactWriteItems’ of undefined at transact (/var/task/node_modules/dynamoose/dist/Dynamoose.js:226:26) at Dynamoose.transaction (/var/task/node_modules/dynamoose/dist/Dynamoose.js:262:13) at process._tickCallback (internal/process/next_tick.js:68:7)
Expected output and behavior:
For the transaction to be carried out.
Environment:
Lambda Nodejs10.x
Operating System: Lambda Amazon Linux
Operating System Version:
Node.js version (node -v
): Lambda runtime node 10.x
NPM version: (npm -v
):
Dynamoose version: 1.11.1
Dynamoose Plugins: None
Dynamoose Plugins:
- Yes, I believe that one or multiple 3rd party Dynamoose plugins are effecting this issue
- [ X] No, I believe this issue is independent of any 3rd party Dynamoose plugins I’m using
- Unknown, I’m unsure if Dynamoose plugins are effecting this issue
- [X ] I am not using any Dynamoose plugins
Other information (if applicable):
Type (select 1):
- [X ] Bug report
- Feature suggestion
- [X ] Question
- Other suggestion
- Something not listed here
Other:
- [X ] I have read through the Dynamoose documentation before posting this issue
- [X ] I have searched through the GitHub issues (including closed issues) and pull requests to ensure this issue has not already been raised before
- [X ] I have searched the internet and Stack Overflow to ensure this issue hasn’t been raised or answered before
- [X ] I have tested the code provided and am confident it doesn’t work as intended
- [X ] I have ensured that all of my plugins that I’m using with Dynamoose are listed above
- [X ] I have filled out all fields above
- [X ] I am running the latest version of Dynamoose
Issue Analytics
- State:
- Created 4 years ago
- Comments:14 (6 by maintainers)
Top GitHub Comments
@fishcharlie Just to circle back for closure in case anyone else runs into this issue when creating micro services with webpack.
The problem was my models lived outside of the directory where my service lived. Importing dynamoose in two places was causing webpack to have two instances in the app. One instance that registered the models, and another to call the transaction.
I fixed this by creating a dbHelper lib that just imports dynamoose, does my env var set up and local checks, and then simply exports the dynamoose instance. Then, everywhere I imported dynamoose, i imported the dbHelper instead and that solved the issue.
Hopefully this helps someone else.
@zeldar Yeah, sorry but I’m not able to run through and help debug that. I’m personally not a fan of build systems like that. They add a lot of complexity and cause the exact problems you are running into now.
At this point tho it doesn’t look like this is a Dynamoose issue. If you want you can try my idea of changing that line to use
this.ddb()
instead ofthis.dynamoDB
. If you do debug further and determine this is indeed a Dynamoose issue please let me know.