Warm tip: This article is reproduced from serverfault.com, please click

Undefined object result from GraphQL api call

发布于 2020-11-28 18:14:36

I'm using React to create a web application. I have a DynamoDB table in AWS and an AppSync API configured.

I'm using the following to make an api call:


const [items, setItems] = useState([]);

  useEffect(() => {
    (async () => {
      const apiGroups = await API.graphql(graphqlOperation(queries.getNtig, { PK: "Status", SK: "Active" }));
      setItems(apiGroups.data.getNtig.Group);
    })();
  }, []);

Later on I use the results to create a dropdown. I had this working perfectly with Rest but I'm trying to switch to using GraphQL.

I see the JSON response in the webconsole:

{
    "data": {
        "getNTIG": {
            "PK": "Status",
            "SK": "Active",
            "Group": [
                "Group1",
                "Group2"
            ]
        }
    }
}

I always get Unhandled Rejection (TypeError): apiGroups.data.getNtig is undefined

Any help greatly appreciated.

Questioner
pwzero
Viewed
0
Emanuele Scarabattoli 2020-11-29 02:43:27

The problem is that the key you are referring to is named getNTIG and not getNtig. The language is case sensitive so it is important to use the right case.