Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dynamodb – How to check if a result is received?

I am fetching data from my dynamo db and would like to know if I am actually receiving anything back.

I’m aware that the Item will always be returned, but I’m not sure what is actually returned when an empty row is returned (ie. the record isn’t there)

const res = await client.send(new GetItemCommand({
    TableName: 'my-table-name',
    Key: {
      ID: { S: id },
    },
  });
);

/*
  Response in form:
  {
    Item: {
      item1: {S: 'hello world'}
    }
  }
*/

Would the Item be an empty object if nothing is returned, would it be Null?
What is the correct method of checking that we actually have a result?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In the JS SDK, Item will not be returned so you can simply check like this:

const res = await client.send(new GetItemCommand({
    TableName: 'my-table-name',
    Key: {
      ID: { S: id },
    },
  });
);

if('Item' in res){
    // Logic here
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading