So I've written the below function. This version is a bit abridged and I've anonymized the data but the critical components are there.
The function basically takes in a list of parameters from an API-Gateway call, queries a db for each of them then returns the results.
I'm finding that the scan runs perfectly with one parameter, but returns duplicate data when more than 1 are called . From the logs I can see that the scans are running multiple times when multiple params are passed
For example, with one param the function logs return
2020-03-19 20:27:42.974 Starting the 0 scan with 3 as the id
2020-03-19 20:27:43.047 The 0 scan has completed successfully
With two params the logs are
2020-03-19 20:28:42.189 Starting the 0 scan with 2 as the id
2020-03-19 20:28:42.261 The 0 scan has completed successfully
2020-03-19 20:28:42.262 Starting the 1 scan with 3 as the id
2020-03-19 20:28:42.267 The 0 scan has completed successfully
2020-03-19 20:28:42.293 The 1 scan has completed successfully
And with 3 params the logs are
2020-03-19 20:29:49.209 Starting the 0 scan with 1 as the id
2020-03-19 20:29:49.323 The 0 scan has completed successfully
2020-03-19 20:29:49.325 Starting the 1 scan with 2 as the id
2020-03-19 20:29:49.329 The 0 scan has completed successfully
2020-03-19 20:29:49.380 The 1 scan has completed successfully
2020-03-19 20:29:49.381 Starting the 2 scan with 3 as the id
2020-03-19 20:29:49.385 The 1 scan has completed successfully
2020-03-19 20:29:49.437 The 2 scan has completed successfully
Here is the code that runs the for loop and the scan. I've hardcoded the parameters and excluded some non-pertinent stuff
const params = ['1','2','3'];
for (let i = 0; i < params.length; i++) {
console.log("Starting the " + i + " scan with " + params[i] + " as the scan parameter")
const scanParams = {
TableName: "Dynamo_Table",
FilterExpression: "Org = :Org",
ExpressionAttributeValues: { ":Org": params[i] },
ProjectionExpression: "User_ID, Org, first_name, last_name"
};
await dynamoClient.scan(scanParams, function(err, data) {
if (err) {
console.log("data retrival failed, error logged is :" + err);
return err;
}
else {
console.log("The " + i +" scan has completed successfully")
//console.log("data retrival successful: " + JSON.stringify(data));
userData = userData.concat(data.Items)
//console.log("partial data structure is " + data)
}
}).promise();
}
responseData = JSON.stringify(userData)
console.log("Complete response is " + responseData)
console.log("data after execution scan is " + data)
I've tried to force the program to wait on the scan's competition by defining a wait and using AWS's .promise() function. However, these don't seem to be blocking the thread execution. I'm not sure exactly why its launching multiple scans though. The for loop isn't running more times than it should, so why is the search function getting called?
Orgthe partition key? Also, you are misusing the SDK functions. You're awaiting something that is not a promise. You've mixed up promises and callbacks. You would useconst data = await dynamoClient.scan(params).promise();, butscanis the wrong solution here.