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 Antwort
1
Akzeptierte Antwort

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
beantwortet vor 2 Jahren
  • 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 :-)

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen