Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot read properties of undefined (reading 'concepts')

I am trying to pull a list of concept names from my neo4j auraDb. I had it working at one point but one I started working on my refetching, something went wrong and now I get an error no matter what I do. The error is occurring in the react component below.

The error messages:

<index.js:165 Uncaught TypeError: Cannot read properties of undefined (reading 'concepts')>
<react_devtools_backend.js:4026 The above error occurred in the AddConcept component:>
<index.js:165 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'concepts')>

The code in question (all in the same file).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The gql and apollo hook:

    import { gql, useMutation, useQuery } from "@apollo/client";
    
    const GET_CONCEPTS = gql`
        query GetConcepts {
          concepts {
            uid
            name
          }
        }
      `;

const { loadingConcepts, errorConcepts, data, refetch } = useQuery(GET_CONCEPTS, {
    fetchPolicy: "network-only",
  });

The react component. The error is being caused here by "data.concepts". :

                 <Autocomplete
                    multiple
                    disabled={loadingConcepts}
                    value={broaderConceptsV || null}
                    onChange={(event, newBroaderConcepts) => {
                      setBroaderConcepts(newBroaderConcepts);
                    }}
                    options={loadingConcepts ? [] : data.concepts}
                    getOptionLabel={(option) => option.name}
                    renderInput={(params) => (
                      <MDInput {...params} variant="outlined" label="Broader Concepts" />
                    )}
                 />

This autocomplete component is within an MUI form with a submit function. The console.log here accurately outputs the array if I put in any valid object for data.concepts above. The code for the submit function:

const handleSubmit = async (e) => {
e.preventDefault();
createConcepts({
  variables: {
    uid: uuidv4(),
    name: nameRef.current.value,
    block: blockV,
    addedBy: user.uid,
  },
});
console.log(data.concepts);
refetch(data); };

>Solution :

Maybe your data is not there yet…. try to wrap your component on a ternary operator like: { data ? <Autocomplete .../> : <p>Test></p>}

Maybe your data is there, but concepts not… so try to use the ternary operator when you try to access data.something

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading