skip to Main Content

I have a dynamo db table where I was able to insert data using node js via lambda. I am able to query from the console and I am also able to query using the cli. When using query with promise its erroring out with invoke error. Its not throwing any specific errors. IF I remove promise and run I can see that connection is successful to the db. I also tried ExpressionAttributeValues: {
":name": {"S":id}
},

even hard coding the value for id and same issue. What am I doing wrong??

import AWS from "aws-sdk"
const dyanamoDB = new AWS.DynamoDB.DocumentClient()

AWS.config.update({ region: "us-east-1" })
export const checkIFIDExist = async (id) => {

    try {
        const params = {
            ProjectionExpression: "String1, String2",
            IndexName: "String2",
            KeyConditionExpression: "String2 = :name",
            ExpressionAttributeValues: {
                ":name": id
            },
            TableName: 'my-table',
        }  
        const data = await dynamoDB.query(params).promise()
        console.log("Data:", data)
        return "success"
    }catch (err) {
        throw new Error (`Failed query for ${id} `, err)
    }

}

Error:

2022-08-16T20:24:09.210Z    c2e0c093-2719-48b8-b0bb-4f38de3ac7b6    ERROR   Invoke Error    
{
    "errorType": "Error",
    "errorMessage": "Failed query for OE0K0I ",
    "stack": [
        "Error: Failed query for OE0K0I ",
        "    at checkIFStepFunctionIDExists (file:///var/task/src/dynamo-query.js:24:15)",
        "    at processTicksAndRejections (internal/process/task_queues.js:95:5)",
        "    at async Runtime.handler (file:///var/task/src/index.js:11:19)"
    ]
}

2

Answers


  1. Chosen as BEST ANSWER

    I basically deleted and trashed the project created new one and did the same stuff I had mentioned earlier in my post and instead of throwing error after catch statement console log it and I am getting the result I expected. I really couldn't tell what was I doing wrong before. @jarmond the error I posted above, I accidentally included a dependency while changing something and caused the error I provided. Thanks everyone for looking into the issue.


  2. If the promise() function doesn’t do what you expect it to do. It’s worth noting, that you can actually also do the same thing with the standard Node.js promisify function.

    import { DocumentClient } from "aws-sdk/clients/dynamodb";
    import { promisify } from "util";
    
    const docClient = new AWS.DynamoDB.DocumentClient()
    ...
    const data = await promisify((cb) => docClient.query(params, cb))();
    

    As @jarmod pointed out, there’s probably something else going on though. I added some sidenotes to clarify some things that you may or may not already know.

    Some sidenotes

    Here are just some remarks which aren’t entirely on topic but which can lead to confusion.

    // don't do this, it doesn't do what you think it does.
    throw new Error(`Failed query for ${id}`, err );
    
    // this prints both a text and an object.
    console.error(`Failed query for ${id}`, err);
    
    // this throws just an error with a message
    throw new Error(`Failed query for ${id}`);
    
    // there is a 2nd argument which you can pass, which is an "options" parameter, which you can use to send a `cause` along.
    throw new Error(`Failed query for ${id}`, { cause: err } );
    

    PS:More details about it can be found it in the MDN documentation.

    I haven’t seen how you got it working without the promise, but if you did it like this, then it’s not what you think.

    try {
        const params = { ... };
        dynamoDB.query(params);
    
        // this point is always reached
        return "success"
    }catch (err) { 
        // this point is never reached
    }
    

    Instead, without a promise, you would have to use a callback function.

    const params = { ... };
    dynamoDB.query(params, (err, data) => {
      if(err) {
        console.error(err);
        return;
      }
      console.log("success", data);
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search