Search code examples
javascriptamazon-dynamodb

How to check if a result is received?


I am fetching data from my DynamoDB 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 (i.e. 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?


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
    }