In my DynamoDB table I've got items with userA (MyUser) following UserB(myFriends..)
Primary-------|---- Srotkey---|---FriendID---|
Myid..... Friend-01 22223333
in the same table I also have the user profiles..
Primary-------|---- Srotkey---|----Name----|
Myid..... Friend-01
22223333 Profile Rose
Now I want to have a a function to return my friend's profile. I suppose my lambda "getFriendProfile" function should do two queries, get the id for the person I'm following in the first query and then use that result to fetch her profile in the second query.
I know how to get those result individually but I don't know how to combine them and put them both into one function.
First query (getPeopleIFollow)
module.exports.handler = async (event, context, callback) => {
const _userID = event.userID;
var params = {
TableName: "mytableName",
ProjectionExpression: "FriendID",
KeyConditionExpression: "#tp = :userId and begins_with(#sk, :skv)",
ExpressionAttributeNames: {
"#tp": "userId",
"#sk": "Sortkey",
},
ExpressionAttributeValues: {
":userId": { "S": _userID },
":skv": { "S": "Friend" },
}
}
var Friend_ID;
try {
let data = await dynamodb.query(params).promise();
data.Items.map(
(dataField) => {
Friend_ID = dataField.FriendID.S,
}
);
callback(null, Friend_ID );
}
catch (error) {
console.log(error);
callback(error);
}
};
My other function looks just looks very much like the first one.. getProfileNames..
module.exports.handler = async (event, context, callback) => {
const _userID = event.myfriendID;
var params = {
TableName: "mytableName",
ProjectionExpression: "Name",
KeyConditionExpression: "#tp = :userId and begins_with(#sk, :skv)",
ExpressionAttributeNames: {
"#tp": "userId",
"#sk": "Sortkey",
},
ExpressionAttributeValues: {
":userId": { "S": _userID },
":skv": { "S": "Profile" },
}
}
try {
let data = await dynamodb.query(params).promise();
const items = data.Items.map(
(dataField) => {
return {
friend_name: dataField.Name.S,
}
}
);
callback(null, items );
}
catch (error) {
console.log(error);
callback(error);
}
};