I'm working on a lambda function that will store information about different users. I have an atribute, userID as my primary partition key, and storedObject as my primary sort key. When I use PutItem, I want it to only add the item if it doesn't already exist in the storedObject attribute.
This is my code
var params = {
TableName: 'TrackItDB',
Item: {
'userID' : {S: currentUser},
'storedObject' : {S: itemName},
'lenderPerson' : {S: personName},
'objectStatus' : {S: 'lent'},
'transactionDate': {S: date},
}
};
....
const checkIfItemIsStoredParams = {
Key: {
"userID" : {
S: currentUser
},
"storedObject" : {
S: itemName
}
},
TableName: "TrackItDB"
};
.....
dynamodb.getItem(checkIfItemIsStoredParams, function(err, data) {
if (!data) {
// no match, add the item
console.log('Item did not exist, storing to DB');
console.log(params);
return dynamodb.putItem(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
}
else {
console.log('Get item succeeded', data);
}
}
});
The problem I'm having is that it always outputs Get Item succeeded to the console even if there is no data. I've tried both if (data) and if (!data) and both return the get item succeeded even when there is no data returned.
console.log('Get item succeeded', data);
not sure how it runs without error though. – Richard Dunn