aws-amplifyamplifyjsaws-amplify-cliaws-amplify-sdk-js

Using Project Expressions in Dynamo DB


I have a DynamoDB table and I am trying to just get one column from the table. More specifically I just want distinct values from a column.

I am using AWS Amplify and have setup an API to query the DynamoDB table.

The get method in the API is as follows. where

  1. tableName - is a variable that contains the name of the table.
  2. Listing_Location is the column I want to retrieve from the table.
app.get(path, function (req, res) {

  if (userIdPresent) {
    req.body['userId'] = req.apiGateway.event.requestContext.identity.cognitoIdentityId || UNAUTH;
  }

  var queryItemParams = {
    TableName: tableName,
    ProjectionExpression: "#listing_location",
    ExpressionAttributeNames = { 
      '#listing_location': 'Listing_Location'
    }
  };

  dynamodb.scan(queryItemParams, (err, data) => {
    if (err) {
      res.statusCode = 500;
      res.json({ error: 'Could not fetch listing locations : ' + err });
    } else {
      res.json(data.Items);
    }
  });
});

from the front end React application I make the following get request, where apiName is the variable which has the API name. path is the api endpoint to make the get request. and I am importing API like this

import { Amplify, API } from 'aws-amplify';
  getAllLocations = () => {
    console.log("in getAllLocations");
    API.get(apiName, path).then(response => {
      console.log(response);
    });
  }

Can you help me understand what I am doing wrong.


Solution

  • cannot believe I made this mistake

    ExpressionAttributeNames = { 
    

    should

    ExpressionAttributeNames : {