Search code examples
javascriptaws-lambdaamazon-dynamodbdynamodb-queries

Problems to query last entry in DynamoDB - Always endup with NULL


I am a beginner in AWS DynamoDb and am stuck now. I am writing a Lambda Function in node.js to query items from DynamoDb. DeviceID is the Partition Key. The idea is to query the last entry. The challenge is that I always have a “null” outcome.

Please help me move forward. Any suggestion, tip,… is more than welcome. Really appreciate that.

Here is the code of my lambda :

const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient()

async function getLastAliine3(deviceID) {
  const params = {
    TableName: 'AliineTable',
    IndexName: 'deviceID-Ux-index',
    key: {
      deviceID: deviceID
    },
    KeyConditionExpression: 'deviceID= :dID',
    ExpressionAttributeValues: {
      ':dID': 'deviceID'
    },
    ScanIndexForward: false,
    Limit: 1
  }


  try {
    const { Item } = await docClient.query(params).promise()
    return Item
  } catch (err) {
    console.log("DDB ERROR MG: ", err)
  }
}

module.exports = getLastAliine3

Solution

  •     ExpressionAttributeValues: {
          ':dID': 'deviceID'
        },
    

    You're passing the string 'deviceId' to the Query instead of the value of your variable. This should fix the problem:

        ExpressionAttributeValues: {
          ':dID': deviceID
        },