Search code examples
reactjsamazon-dynamodbaws-sdk-js

Can you use aws-sdk/util-dynamodb unmarshall in Reactjs?


I have an API Gateway resource which calls a Dynamodb post request to query a table. I'm trying to call the API within my React app using Axios. The API is working, returning the data as expected (console log) but I'm getting errors if I try to use @aws-sdk/util-dynamodb (unmarshall) to convert the api data.items response into JSON and use within React.

./node_modules/@aws-sdk/util-dynamodb/dist-es/convertToNative.js 45:14 Module parse failed: Unexpected token (45:14)

Is there a way to use 'unmarshall' within React? Something like this:

    useEffect(() => {
    const getGoal = async () => {
        try {
            const response = await api.get(apiUrl)
            setGoal(unmarshall(response.data.Items))

This works if I use a Lambda service, but I'm try to see if I can reduce my code.


Solution

  • While I'm not sure on the issue you are getting with unmarshall function, I would suggest using the DynamoDB Document Client which will return your data already unmarshalled.

    The DynamoDB Document client simplifies working with items by abstracting the notion of attribute values. This abstraction annotates native JavaScript types supplied as input parameters, and converts annotated response data to native JavaScript types.