Reputation: 548
I have the following gQL defined,
import gql from 'graphql-tag'
const SIGN_UP_QUERY = gql`
query {
signUpForm @client {
firstName
email
password
}
}
`
and i use it with react-apollo query as such
<Query query={SIGN_UP_QUERY}>
{({
data: {
signUpForm: { firstName }
},
loading
}) => { ...... }}
</Query>
With this i am getting an error Cannot read property 'firstName' of undefined
What is it that i am doing wrong here?
Upvotes: 1
Views: 12827
Reputation: 1325
Since you are destructuring variables, you need to make sure that data
, also data.signUpForm
are null OR undefined.
Here is an example for your situation.
There are three problems can make data/data.signUpForm
is not an object:
undefined
To resolve your problem:
Upvotes: 1
Reputation: 8775
You're de-structuring the data
variable before it exists/has loaded. You should wait until loading = false before trying to access the data.
Upvotes: 6
Reputation: 196
Do you check if data has loaded yet?
if (loading) return "data not fetched yet";
return <p>{ firstName }</p>;
Upvotes: 3