-1

i'm making an API in AWS Lambda with NodeJS and DynamoDB and'm trying to get details of a specific customer or customers who comes from Colorado (state = Colorado).

Here i was trying to get all customers from Colorado.

router.get("/customers", (req, res) => {
    const params = {
      TableName: 'Customer',
      FilterExpression: "stateOrProvince = :state",
      ExpressionAttributeValues: {
        ":state": "Colorado"
      }
    };
    dynamoDb.query(params, (error, result) => {
      if (error) {
        res.status(400).json({ error: `Error fetching the ${tableName}` });
      }
      res.json(result.Items);
    });
});

Here i was trying to get details from a specific customer

router.get("/customers/:customerId", (req, res) => {
    const id = req.params.customerId
    const params = {
      TableName: 'Customer',
      FilterExpression: "customerId = :custId",
      ExpressionAttributeValues: {
        ":custId": id
      }
    };
    dynamoDb.query(params, (error, result) => {
      if (error) {
        res.status(400).json({ error: `Error fetching the ${tableName}` });
      }
      res.json(result.Items);
    });
});

Both cases i'm getting a 502 error in postman :|

{ "message": "Internal server error" }

Could you help me to identify what the issue is ? I've read and tried differents syntaxes but I couldn't get it to work yet Thanks

1
  • possible to share your attributes data types and check whether you are passing correct data type ? and also can you check are you using documentClient?\ Commented Sep 2, 2022 at 8:29

1 Answer 1

1

You cannot use Query operation without using a KeyConditionExpression which you have not defined. You must at least insert the partition key for a Query.

Try below, which uses Scan instead:

router.get("/customers", (req, res) => {
    const params = {
      TableName: 'Customer',
      FilterExpression: "stateOrProvince = :state",
      ExpressionAttributeValues: {
        ":state": "Colorado"
      }
    };
    dynamoDb.scan(params, (error, result) => {
      if (error) {
        res.status(400).json({ error: `Error fetching the ${tableName}` });
      }
      res.json(result.Items);
    });
});

Or for a specific customer, set KeyConditionExpression:

router.get("/customers/:customerId", (req, res) => {
    const id = req.params.customerId
    const params = {
      TableName: 'Customer',
      KeyConditionExpression: "customerId = :custId",
      ExpressionAttributeValues: {
        ":custId": id
      }
    };
    dynamoDb.query(params, (error, result) => {
      if (error) {
        res.status(400).json({ error: `Error fetching the ${tableName}` });
      }
      res.json(result.Items);
    });
});

More info here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.