title: Combining multiple queries in one aws lambda function
body: <p>In my DynamoDB table I've got items with userA (MyUser) following UserB(myFriends..) </p>
  
  <pre><code>Primary-------|---- Srotkey---|---FriendID---|
   Myid.....       Friend-01        22223333
  </code></pre>
  
  <p>in the same table I also have the user profiles..</p>
  
  <pre><code>Primary-------|---- Srotkey---|----Name----|
   Myid.....       Friend-01
   22223333          Profile         Rose    
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>I know how to get those result individually but I don't know how to combine them and put them both into one function.</strong>  </p>
  
  <p>First query (getPeopleIFollow)</p>
  
  <pre><code>module.exports.handler = async (event, context, callback) =&gt; {
  
      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) =&gt; {
  
    Friend_ID = dataField.FriendID.S,
  
  
    }
  
  );
  
     callback(null, Friend_ID );
  
    }
    catch (error) {
      console.log(error);
  
      callback(error);
    }
  
  };
  </code></pre>
  
  <p>My other function looks just looks very much like the first one..
  getProfileNames..</p>
  
  <pre><code>module.exports.handler = async (event, context, callback) =&gt; {
  
      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) =&gt; {
  
        return {
          friend_name: dataField.Name.S,
  
      }
  
    }
  
  
  );
  
     callback(null, items );
  
    }
    catch (error) {
      console.log(error);
  
      callback(error);
    }
  
  };
  </code></pre>