question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get all Items using the Query API through AWS Amplify ('LastEvaluatedKey')

See original GitHub issue

** Which Category is your question related to? ** AWS Amplify GET API ** What AWS Services are you utilizing? ** Amplify, NodeJS ** Provide additional details e.g. code snippets **

How can we get all the items by invoking dynamodb.query?

The documentation states that we need to look for the presence of ‘LastEvaluatedKey’. Just wondering how we could aggregate all the Items in an efficient way?

app.get(path, function (req, res) {
    var allItems = [];
    var params = {
        TableName: tableName,
        "IndexName": "status-index",
        "KeyConditionExpression": "#attrib_name = :attrib_value",
        "ExpressionAttributeNames": { "#attrib_name": "status" },
        "ExpressionAttributeValues": { ":attrib_value": req.query.status },
        "ScanIndexForward": false
    }

    dynamodb.query(params, onQuery);

    function onQuery(err, data) {
        if (err) {
            res.json({ error: 'Could not load items: ' + err });
        } else {
            // Should I be aggregating all the items like this?
            allItems = allItems.concat(data.Items);

            // Then should I set it to res like this to return all the items?
            res.json(allItems);

            if (typeof data.LastEvaluatedKey != 'undefined') {
                params.ExclusiveStartKey = data.LastEvaluatedKey;
                dynamodb.query(params, onQuery);
            }
        }
    }
});

Please look at comments within the code. That is where I think we need to have the appropriate code to aggregate all the items and return back the response.

I have written NodeJS code to insert items good enough to see that LastEvaluatedKey is returned in the response. However, I have not found a way to debug this over the DynamodDB on AWS online yet as I’m fairly new to DynamoDB and AWS Amplify. Let me as well know if there is an easier way to debug this in an AWS amplify backed up GET API. Not sure how to point the amplify app to local DynamoDB to test locally with it. How do you folks debug in general?

Please ignore the above paragraph if it is digressing from the main issue. Just need some immediate help with the query part though!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
stayingcoolcommented, Sep 7, 2019

@sammartinez Yes I’m using REST APIs created through Amplify. GraphQL wasn’t there when I started with Amplify.

Thank you for your suggestions on aggregating the dynamodb items, I’ll go with option 1.

I’m surprised when you say NodeJS is not officially supported for Amplify. The Amplify CLI for RESTful APIs creates the NodeJS code, isn’t it? Where can I find the supportability matrix for Amplify JS?

0reactions
github-actions[bot]commented, Jun 12, 2021

This issue has been automatically locked since there hasn’t been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get all Items using the Query API through AWS Amplify
Query returns paginated results - if you want all data then you need to keep querying and aggregating until your LastEvaluatedKey is empty....
Read more >
Query - Amazon DynamoDB
A Query operation can return an empty result set and a LastEvaluatedKey if all the items read for the page of results are...
Read more >
Paginating table query results - Amazon DynamoDB
The LastEvaluatedKey in the response indicates that not all of the items have been retrieved. The AWS CLI then issues another Query request...
Read more >
Working with scans in DynamoDB
The LastEvaluatedKey in the response indicates that not all of the items have been retrieved. The AWS CLI then issues another Scan request...
Read more >
Getting started with DynamoDB and the AWS SDKs
In this getting started guide, learn the basic concepts of Amazon DynamoDB and working the various AWS SDKs to start using DynamoDB from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found