0

I created a lambda function and connected it to the AWS API Gateway to get my DynamoDB table data in JSON format for the front end, I am trying to get my column entries from my DynamoDB table as a List in JSON format but what I end up getting is a complete String of all the entries in my table which has practically zero usage for the front end (Flutter).

Here's my lambda function

const AWS = require("aws-sdk");
const documentClient = new AWS.DynamoDB.DocumentClient();

exports.handler = async event => {
  const params = {
    TableName: "//tableName" 
  };
  try {
    // Utilising the scan method to get all items in the table
    const data = await documentClient.scan(params).promise();
    const response = {
      statusCode: 200,
      body: JSON.stringify(data.Items)
    };
    return response;
  } catch (e) {
    return {
      statusCode: 500
    };
  }
};

My API's decoded JSON Body

{statusCode: 200, body: [{"product":"clear","__typename":"ProductModel","size":"small","_lastChangedAt":1625829002606,"_version":1,"company":"add","updatedAt":"2021-07-09T11:10:02.578Z","category":"Pot","createdAt":"2021-07-09T11:10:02.578Z","price":"69","description":"this doesn't even make sense","id":"f4acb29c-f8f3-4765-abf6-aef0002a837c"},{"product":"product","__typename":"ProductModel","size":"1","_lastChangedAt":1625988107746,"_version":1,"company":"company","updatedAt":"2021-07-11T07:21:47.704Z","category":"Climbers","createdAt":"2021-07-11T07:21:47.704Z","price":"221","description":"description","id":"0a51d3c3-4df3-4549-abaa-f8b88e8ac407"}]
}

I think there's a problem in my lambda function as the body part in it does not return a list but I can't get it working. Any help will be appreciated

Suyash_singh
  • 43
  • 2
  • 5
  • What do you mean? There are two product entries in that `body` array / list. – luk2302 Jul 13 '21 at 05:53
  • I am trying to return the contents of the body as a list. (I want my columns of the table to return as a list rather than a String) . More info here: https://stackoverflow.com/questions/68353249/unhandled-exception-type-string-is-not-a-subtype-of-type-listdynamic – Suyash_singh Jul 13 '21 at 06:14
  • 2
    The `body` ***IS*** a list. What specific output do you want and why. Right after the *"My API's decoded JSON Body"* post the *desired* output instead of saying string instead of list or the other way around. – luk2302 Jul 13 '21 at 10:21
  • @Suyash_singh The way you're returning that data from the lambda is correct, as the JSON must be stringified to return via API Gateway. How that stringified JSON is handled by the client is up to the client. Maybe add `headers: { 'Content-Type': 'application/json' }` to your response to indicate it's JSON. – 404 Jul 14 '21 at 12:29

0 Answers0