O'Reilly logo

Serverless Single Page Apps by Ben Rady

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Fetching Documents

Now that we put a document in DynamoDB, let’s get it back out again. We can reuse sendDbRequest to create a function that fetches a saved answer asynchronously. It should look something like this:

 learnjs.fetchAnswer = ​function​(problemId) {
 return​ learnjs.identity.then(​function​(identity) {
 var​ db = ​new​ AWS.DynamoDB.DocumentClient();
 var​ item = {
  TableName: ​'learnjs'​,
  Key: {
  userId: identity.id,
  problemId: problemId
  }
  };
 return​ learnjs.sendDbRequest(db.get(item), ​function​() {
 return​ learnjs.fetchAnswer(problemId);
  })
  });
 };

Just as with saveAnswer, we need to start by fetching the Cognito ID. Once we have it, we can create the request object, which has ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required