AWS-SDK-Javascript v3 API call to DynamoDB is return undefined and ignoring execution of a console.log command
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:
- ddbGetAllRoomConnections-1: is coming correctly before the ddbClient.send command
- after the ddbClient.send command there is no ddbGetAllRoomConnections-2 log entry
- 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.
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
Happy to help :-) Can you accept the answer? It gives me 10 points in stead of 1 :-)
Relevant questions
How to query all items of a specific type?
asked 3 months agoI would like to know easy way to start from the first line of log for the code-build execution (either in code-build console or cloudwatch-log console)
asked 2 months agoReturn Count from Lambda into the "Response Header" of the API
Accepted Answerasked 2 years agojsii.errors.JSIIError: Cannot read properties of undefined (reading 'bindToGraph')
asked 6 months agoMultiple chat widgets using out-of-the-box client?
Accepted Answerasked 19 days agoJavascript IoT client SDK AttachPolicy call inside AWS lambda blocks and causes the lambda to timeout without any error/exception/log whatsoever
Accepted Answerasked 6 months agoAWS-SDK-Javascript v3 API call to DynamoDB is return undefined and ignoring execution of a console.log command
Accepted Answerasked a month agoTrying to call API with a list of URLs but Lambda is timing out
asked 24 days agoHow to increase expiration of NextToken in DynamoDB ExecuteStatement?
asked 16 days ago[API Gateway] What is the maximum size of the connection ID used in @connections commands?
asked 3 months ago
So obvious, that I didn't see it. Thanks for pointing out.