Reputation: 89
I am seeing unexpected behavior when calling Promise.all()
. I can't seem to get the desired data. When the promise is called directly the data returns just fine, but calling Promise.all()
throws following error:
2018-04-09T16:31:35.827Z 77ea5111-3c13-11e8-9800-a5b8cf1bf96f (node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Cannot read property 'streak' of undefined
This is the relevant code:
function getUserData(UserId) {
const docClient = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: "XXXXXXXXXX",
Key: {
"UserId": UserId,
}
};
return docClient.get(params).promise();
}
function buildResponse (UserId, weekNumber, yesterday, today, lastLogin) {
let currentStreak
getUserData(UserId);
getUserData(UserId).then((data) => {
currentStreak = data.Item.streak;
console.log("Streak is " + currentStreak); //Successfully returns "Streak is 22"
});
Promise.all([getUserData(UserId)]).then((data) => { //Throws error shown below
var test = data.Item.streak;
console.log("Promise is " + test);
});
}
Note: I would like to add multiple promises in the future but I am just starting with one for now.
Upvotes: 0
Views: 79
Reputation: 1732
As @Hunter pointed out, Promise.all
resolves with an array of all individual promise results. Arrays don't have an Item
property, so data.Item
is undefined, thus accessing data.Item.streak
gives you the error you're seeing.
It doesn't look like you should be using Promise.all()
, though. You're only actually waiting on one promise, and unnecessarily wrapping it in an array. Try just taking it out, along with the array brackets:
getUserData(UserId).then((data) => {
var test = data.Item.streak;
console.log("Promise is " + test);
});
Upvotes: 0
Reputation: 61512
Promise.all()
returns an array, you need to destructure the response:
Promise.all([getUserData(UserId)]).then(([data]) => {
var test = data.Item.streak;
console.log("Promise is " + test);
});
or:
Promise.all([getUserData(UserId)]).then((response) => {
var [data] = response;
var test = data.Item.streak;
console.log("Promise is " + test);
});
or extract the data by index:
Promise.all([getUserData(UserId)]).then((response) => {
var data = response[0];
var test = data.Item.streak;
console.log("Promise is " + test);
});
additionally you should have .catch() blocks to ensure that errors don't bubble up.
Upvotes: 2