AWS-SDK-Javascript v3 API call to DynamoDB is return undefined and ignoring execution of a console.log command

0

The goal of this code snippet is retreiving all connectionsids of a chat room to reply to a chat sendmessage command in the API Gateway WebSocket. I have used PutCommand and GetCommand a lot, but today I'm using the QueryCommand for the first time. The code Part 1, the DynamoDB call:

export async function ddbGetAllRoomConnections(room) {
  const params = {
        "TableName": "MessageTable",
        "KeyConditionExpression": "#DDB_room = :pkey",
        "ExpressionAttributeValues": {
          ":pkey": ""
        },
        "ExpressionAttributeNames": {
          "#DDB_room": "room"
        },
        "ScanIndexForward": true,
        "Limit": 100
  };
  console.log("ddbGetAllRoomConnections-1:",params);
  const data = await ddbClient.send( new QueryCommand(params) );
  console.log("ddbGetAllRoomConnections-2:",data);
  return data;
}

The calling part:

            const normalConnections = ddbGetAllRoomConnections(connData.lastroom);
            if (typeof normalConnections.Items === 'undefined' || normalConnections.Items.length <= 0) {
              throw new Error("Other Connections not found");
            }

The following logfile entries are occuring in sequence:

logfile puzlle message1:
ddbGetAllRoomConnections-1: {
  TableName: 'MessageTable',
  KeyConditionExpression: '#DDB_room = :pkey',
  ExpressionAttributeValues: { ':pkey': '' },
  ExpressionAttributeNames: { '#DDB_room': 'room' },
  ScanIndexForward: true,
  Limit: 100
}

logfile puzlle message2:
ERROR	Error: Other Connections not found
    at Runtime.handler (file:///var/task/chat-messages.js:49:21)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  statusCode: 500
}

logfile puzlle message3:
END RequestId: 

Waht irritates me is, the following sequence of occurences in the logfile:

  1. ddbGetAllRoomConnections-1: is coming correctly before the ddbClient.send command
  2. after the ddbClient.send command there is no ddbGetAllRoomConnections-2 log entry
  3. The next logentry is after the call of ddbGetAllRoomConnections showing the value undefined. I tried also PartiQL per ExecuteCommand, then debugging with Dynobase I retrieved the code for the params section in the current setting.
1 Answer
1
Accepted Answer

I think in the calling part you should also await:

const normalConnections = ddbGetAllRoomConnections(connData.lastroom);

should be

const normalConnections = await ddbGetAllRoomConnections(connData.lastroom);

Your code is valid but normalConnections will be a promise and not a result

profile picture
JaccoPK
answered 2 years ago
  • So obvious, that I didn't see it. Thanks for pointing out.

  • Happy to help :-) Can you accept the answer? It gives me 10 points in stead of 1 :-)

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions