Reputation: 316
There is a DynamoDB table called Portal.
From a Lambda function written in node I want to update the status
and ts
fields of an item (by calling a signalJobStart function) and wait for the async update function to finish with a promise before continuing.
On some reason no update is happening, however no errors are raised during the operation.
There is no error in the logs, and I can see the "FINISHED" log message in the logs.
Why there is no change made on the item in the table? Why I cannot see neither the error nor the success message in the logs?
(I also tried without the promise, with the same outcome. The table item does not get updated - even asynchronously.)
Here is the code from the Lambda:
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'eu-west-1'});
module.exports = {
signalJobStart: function(accountId, jobId) {
console.log("Signaling job start for account %s and job %s", accountId, jobId);
let table = "Portal";
let params = {
TableName: table,
Key:{
"accountid": accountId,
"entity": jobId
},
UpdateExpression: "SET status = :s, ts = :t",
ExpressionAttributeValues:{
":s": "running",
":t": Date.now()
},
ReturnValues:"UPDATED_NEW"
};
let updatePromise = docClient.update(params, function(err, data) {
if (err) {
console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
}
}).promise();
updatePromise.then(function(data) {
console.log('Success');
}).catch(function(err) {
console.log(err);
});
console.log("FINISHED");
}};
Upvotes: 1
Views: 4992
Reputation: 281
What helped in my case:
Do in async/await way:
exports.handler = async (event) => {
...
let _result = "";
try {
_result = await docClient.update(_updateParams).promise();
} catch(ex){
_result = ex;
}
..
Make sure the field I was writing to is not a reserved word like in the following case ("data" is reserved word):
{
..
UpdateExpression: "set data = :newdata",
ExpressionAttributeValues: {
":newdata": "blah"
},
..
To check this: rename column to, say, "data2" - it worked no problem.
Make sure you have permissions in "Execution Role" in IAM Console.
Upvotes: 5
Reputation: 3729
In your query status
is reserve keywords and prohibited in dynamodb. So use ExpressionAttributeNames
in Your query.
Also use new Promise for handle promise
Please check below its help you:
module.exports = {
signalJobStart: function (accountId, jobId) {
return new Promise(function (resolve, reject) {
console.log("Signaling job start for account %s and job %s", accountId, jobId);
let table = "Portal";
let params = {
TableName: table,
Key: {
"accountid": accountId,
"entity": jobId
},
UpdateExpression: "SET #status = :s, ts = :t",
ExpressionAttributeNames: {
'#status': 'status'
},
ExpressionAttributeValues: {
":s": "running",
":t": Date.now()
},
ReturnValues: "UPDATED_NEW"
};
docClient.update(params, function (err, data) {
if (err) {
console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
reject(err)
} else {
console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
resolve({ message: 'sucsss', data: data })
}
})
});
}
};
Note: Please also confirm
accountid
andentity
are bothKey
in you DB.
Upvotes: 0
Reputation: 2228
I think you could
docClient.update(params).promise()
and then continue to use the .then(...)
statements flow to make it work.
Personally, I think the error handler syntax (for update
) might not yield you a promise.
Let me know if that does not works.
Upvotes: 2