'My promise is undefined when getting dynamodb info for a key
I get undefined whenever I return the promise.
Async Function to retrieve Item from ddb
async function getTask(){
const userName = 'jared'; //key name
const userTable = 'todoUsers'; //table name
async function getTask(){ //get list of tasks for key
const params = { //params for dynamodb.get
Key:{
"username": userName
},
TableName: userTable,
ConsistentRead: true,
};
return await dynamodb.update(params).promise().then(response => { //returning response
return response;
}, error =>{
console.log('There is an error getting task: ', error);
});
}
Solution 1:[1]
The code you have provided seems to be incorrect, since you define getTask() twice. I also don't understand why you await and then use a then.
Try this and see if the return is defined:
public static async getTask() {
const userName = 'jared'; //key name
const userTable = 'todoUsers'; //table name
const params = {
//params for dynamodb.get
Key: {
username: userName
},
TableName: userTable,
ConsistentRead: true
};
return documentClient.update(params).promise();
}
Solution 2:[2]
It worked after I used try catch.
async function getTasks(){
const userName = 'jared';
const userTable = 'todoUsers';
try {
var params = {
KeyConditionExpression: 'username = :user',
ExpressionAttributeValues: {
':user': userName
},
TableName: userTable
};
var result = await dynamodb.query(params).promise()
return JSON.stringify(result.Items);
} catch (error) {
console.error(error);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ??? |
| Solution 2 | jaredl88 |
