Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamoDB - Cannot read property '0' of undefined #2117

Closed
antoineaws opened this issue Mar 9, 2021 · 5 comments
Closed

DynamoDB - Cannot read property '0' of undefined #2117

antoineaws opened this issue Mar 9, 2021 · 5 comments
Labels
closed-for-staleness guidance General information and guidance, answers to FAQs, or recommended best practices/resources.

Comments

@antoineaws
Copy link

Describe the bug

The @aws-sdk/client-dynamodb is throwing an ambiguous error 'Cannot read property '0' of undefined' . I am using sdk v3.8.0. It was working great with the previous sdk using DocumentClient instead.

Your environment

SDK version number

@aws-sdk/client-dynamodb@3.8.0

Is the issue in the browser/Node.js/ReactNative?

Node.js

Details of the browser/Node.js/ReactNative version

Paste output of npx envinfo --browsers or node -v or react-native -v
v14.15.4

Steps to reproduce

Run the following command to start local debugging:
sam local start-api -d 9999

Create a simple dynamo db table and a secondary index.
Create a lambda function with the following code:

const docClient = new DynamoDB({ region: 'ca-central-1' });
const { Items } = await docClient.query({
            TableName: 'MyTable',
            IndexName: 'MyColumnIndex',
            KeyConditionExpression: "MyColumn = :myColumn",
            ExpressionAttributeValues: {
                ":myColumn": parsedBody.myColumn
            },
        });

The table is already created and the index as well. The previous version of the sdk worked fine.

Observed behavior

A clear and concise description of what happens.
An ambiguous error is thrown.
Invalid lambda response received: Invalid API Gateway Response Keys: {'errorType', 'trace', 'errorMessage'} in {'errorType': 'TypeError', 'errorMessage': "Cannot read property '0' of undefined", 'trace': ["TypeError: Cannot read property '0' of undefined", ' at Object.visit (/var/task/node_modules/@aws-sdk/client-dynamodb/dist/cjs/models/models_0.js:1101:40)', ' at serializeAws_json1_0AttributeValue (/var/task/node_modules/@aws-sdk/client-dynamodb/dist/cjs/protocols/Aws_json1_0.js:4612:38)', ' at /var/task/node_modules/@aws-sdk/client-dynamodb/dist/cjs/protocols/Aws_json1_0.js:5074:20', ' at Array.reduce (<anonymous>)', ' at serializeAws_json1_0ExpressionAttributeValueMap (/var/task/node_modules/@aws-sdk/client-dynamodb/dist/cjs/protocols/Aws_json1_0.js:5068:34)', ' at serializeAws_json1_0QueryInput (/var/task/node_modules/@aws-sdk/client-dynamodb/dist/cjs/protocols/Aws_json1_0.js:5544:40)', ' at Object.serializeAws_json1_0QueryCommand (/var/task/node_modules/@aws-sdk/client-dynamodb/dist/cjs/protocols/Aws_json1_0.js:354:27)', ' at serialize (/var/task/node_modules/@aws-sdk/client-dynamodb/dist/cjs/commands/QueryCommand.js:95:30)', ' at /var/task/node_modules/@aws-sdk/middleware-serde/dist/cjs/serializerMiddleware.js:5:27', ' at /var/task/node_modules/@aws-sdk/middleware-logger/dist/cjs/loggerMiddleware.js:6:28']}

Expected behavior

Expected the records to be returned.

Screenshots

N/A

Additional context

N/A

@antoineaws antoineaws added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 9, 2021
@antoineaws
Copy link
Author

I figured it out, the query "ExpressionAttributeValues" were not formatted properly. Maybe we should adjust the typing in typescript to flag this as an error?

The correct way is: note the { S: parsedBody.myColumn }

const { Items } = await docClient.query({
            TableName: 'MyTable',
            IndexName: 'MyColumn',
            KeyConditionExpression: "MyColumn = :myColumn",
            ExpressionAttributeValues: {
                ":myColumn": { S: parsedBody.myColumn }
            },
        });

@ajredniwja
Copy link
Contributor

Hey @antawad, thanks for opening this issue, I believe its more of a client side usage mistake as when using DynamoDb DocumentClient you dont have to mention the attribute type with ExpressionAttributeValues, while with the normal client you would.

The documentClient has been implemented in this version of the SDK as well, see #2097

@ajredniwja ajredniwja added guidance General information and guidance, answers to FAQs, or recommended best practices/resources. response-requested Waiting on additional info and feedback. Will move to \"closing-soon\" in 7 days. and removed bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 9, 2021
@jeanlescure
Copy link

I had the same issue as @antawad and the Cannot read property '0' of undefined error really gives no pointer as to what us as clients are doing wrong. I agree with his suggestion to update the typescript definition to enforce the valid variations of the S: AttributeValue type signature (i.e 'B' | 'BOOL' | 'BS' | 'L' | 'M' | 'N' | 'NS' | 'NULL' | 'S' | 'SS'.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to \"closing-soon\" in 7 days. label Mar 11, 2021
@github-actions
Copy link

Greetings! We’re closing this issue because it has been open a long time and hasn’t been updated in a while and may not be getting the attention it deserves. We encourage you to check if this is still an issue in the latest release and if you find that this is still a problem, please feel free to comment or open a new issue.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Mar 12, 2022
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
closed-for-staleness guidance General information and guidance, answers to FAQs, or recommended best practices/resources.
Projects
None yet
Development

No branches or pull requests

3 participants