0
votes

I have a dynamodb query in nodejs as follows. I have a matching record but its not returning any values. Any idea to resolve the problem?

const docClient = new AWS.DynamoDB.DocumentClient({ region: "ap-south-1" });
var userParams = {
        TableName: "aa-users",
        FilterExpression: '#email = :emailval AND #postProfileOTP = :otpval',
        ExpressionAttributeNames: {
              "#email": "email",
              "#postProfileOTP": "postProfileOTP"
          },
        ExpressionAttributeValues: {
            ":emailval": email,
            ":otpval": otp
        }
    };
    docClient.scan(userParams, function (err, data) {
         console.log(data.Count) // not getting the result count
         if(data.Count > 0){
          res.status(200).json({ "status": 1, "data": data.Items })
         }
    })

Instead am getting the error as follows:

2020-01-24T11:56:20.554Z ee0fd306-6bd5-4cf0-957e-732b08c6b0e3 ERROR Uncaught Exception { "errorType": "TypeError", "errorMessage": "Cannot read property 'Count' of null", "code": "TypeError", "message": "Cannot read property 'Count' of null", "time": "2020-01-24T11:56:20.554Z", "stack": [ "TypeError: Cannot read property 'Count' of null", " at Response. (/var/task/handler.js:351:18)", " at Request. (/var/task/node_modules/aws-sdk/lib/request.js:364:18)", " at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:106:20)", " at Request.emit (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:78:10)", " at Request.emit (/var/task/node_modules/aws-sdk/lib/request.js:683:14)", " at Request.transition (/var/task/node_modules/aws-sdk/lib/request.js:22:10)", " at AcceptorStateMachine.runTo (/var/task/node_modules/aws-sdk/lib/state_machine.js:14:12)", " at /var/task/node_modules/aws-sdk/lib/state_machine.js:26:10", " at Request. (/var/task/node_modules/aws-sdk/lib/request.js:38:9)", " at Request. (/var/task/node_modules/aws-sdk/lib/request.js:685:12)" ] }

2

2 Answers

1
votes

It looks like AWS is returning an err rather than results/data. When you receive a callback that has an "err" parameter as the first argument, you should always check that this is not set before continuing. Eg:

if (err) {
   console.error(err)
   // recover in some way, or kill process
   process.exit(1)
}

By doing this, the error being returned from AWS will be logged to the console, and you'll have a much better idea of what is going wrong.

1
votes

simply log out the error thrown by dynamoDb. We will know the reason.

Like this

`

docClient.scan(userParams, function (err, data) {
      if(err) console.log(err)
         if(data.Count > 0){
          res.status(200).json({ "status": 1, "data": data.Items })
         }

`