Solution: "data" from the Apollo query was not arriving on time for a check in the options property of the Autocomplete component, even though loading was complete. My final code for the Autocomplete component:
<Autocomplete
multiple
disabled={loadingConcepts}
value={narrowerConceptsV || null}
onChange={(event, newNarrowerConcepts) => {
setNarrowerConcepts(newNarrowerConcepts);
}}
the solution >>>> options={!loadingConcepts && data ? data.concepts : []}
getOptionLabel={(option) => option.name}
renderInput={(params) => (
<MDInput {...params} variant="outlined" label="Narrower Concepts" />
)}
/>
Original Post:
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).
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); };
CodePudding user response:
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