Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello, just curious why is this error appearing after everything seems alright?
React typescript, any suggestions?

https://snipboard.io/eVzvP7.jpg[^]

What I have tried:

console.log(data);

async function fetchOperatingAreas() {
   let result = await WssoService.getQualityContracts(
     operatorsId,
     type,
     category
   );
   if (result?.data) {
     if (
       !result.data.key &&
       (result.data.json === undefined || result.data.json === null)
     ) {
       result = await WssoService.createQualityContracts(
         operatorsId,
         type,
         category
       );
     }
     if (result.data.json !== null && result.data.json !== undefined) {
       if (result.data.json === '') {
         result.data.json = '{}';
       }
       setFetchedData(
         mergeDefaultWithFetchedKvp(JSON.parse(result.data.json))
       );
     }
     setQualityContracts(result?.data || []);
   }

   setValue(result.data);
 }


useEffect(() => {
  if (value) {
    setData(value?.data ?? []);
  }
}, [value]);
console.log(data);
Posted
Updated 17-Apr-21 0:12am
Comments
Richard MacCutchan 17-Apr-21 5:43am    
What is the problem, and where does it occur?
Valentin Ganchev 17-Apr-21 6:11am    
Property 'data' does not exist on type 'never' occures after running the code.

Dave Kreskowiak 17-Apr-21 12:52pm    
You have to take a real close look at the json that was returned. Google for "json pretty print" for online tools that will do that for you.

Pay close attention to the hierarchy of the property names in the structure. Once you find your "never" property, you will find that there is no property one level under it that is called "data".

You may find "data" somewhere else, but it won't be under the "never" property.

1 solution

JavaScript
const [data, setData] = useState<any[]>([]);
 const [kvp, setFetchedData] = useState<any>();
 const [value, setValue] = useState();
 const [localDefaultValues, setLocalDefaultValues] = useState([
   ...defaultValues,
 ]);

 const handleChange = (event: { target: { value: any; name: string } }) => {
   const newValue = event.target.value;
   const key = event.target.name;
   const newData = [...kvp];
   const elementToChange = newData.filter(x => x.key === key)[0];
   elementToChange.value = newValue;

   setFetchedData(mergeDefaultWithFetchedKvp(newData));
 };

 const handleSelectChange = (event: any) => {
   const yearsKey = event.target.name;
   const prefixYears = yearsKey.split('-')[0];
   const newYearsKey = event.target.value;

   const newData = [...kvp];

   const elementToChange = newData.filter(x => x.key === yearsKey)[0];
   elementToChange.key = prefixYears + '-' + newYearsKey;

   const newLocalDefaultValues = [...localDefaultValues];
   const index = newLocalDefaultValues.indexOf(yearsKey);
   newLocalDefaultValues[index] = elementToChange.key;

   setLocalDefaultValues(newLocalDefaultValues);
   setFetchedData(newData);
 };

 async function fetchOperatingAreas() {
   let result = await WssoService.getQualityContracts(
     operatorsId,
     type,
     category
   );
   if (result?.data) {
     if (
       !result.data.key &&
       (result.data.json === undefined || result.data.json === null)
     ) {
       result = await WssoService.createQualityContracts(
         operatorsId,
         type,
         category
       );
     }
     if (result.data.json !== null && result.data.json !== undefined) {
       if (result.data.json === '') {
         result.data.json = '{}';
       }
       setFetchedData(
         mergeDefaultWithFetchedKvp(JSON.parse(result.data.json))
       );
     }
     setQualityContracts(result?.data || []);
   }

   setValue(result.data);
 }

 useEffect(() => {
   if (operatorsId) {
     fetchOperatingAreas();
   }
   /* eslint-disable react-hooks/exhaustive-deps */
 }, [operatorsId]);

 const mergeDefaultWithFetchedKvp = (fetchedKvp: any) => {
   const totalKvp = new Array<any>();
   const defaultKvp = new Array<any>();
   localDefaultValues.forEach((x: any) => {
     defaultKvp.push({ key: x });
   });

   defaultKvp.forEach((x: any) => {
     const fieldKey = x.key;
     let fetchedKvpPair = null;

     if (fetchedKvp.length > 0) {
       fetchedKvpPair = fetchedKvp.filter((y: any) => y.key === fieldKey)[0];
     }

     if (fetchedKvpPair) {
       totalKvp.push({ key: fieldKey, value: fetchedKvpPair.value });
     } else {
       totalKvp.push({ key: fieldKey, value: null });
     }
   });
   if (fetchedKvp.length > 0) {
     fetchedKvp.forEach((x: any) => {
       if (
         totalKvp.filter((y: any) => y.key === x.key).length === 0 &&
         x.key &&
         x.value
       ) {
         if (x.key.startsWith('years')) {
           const leftPart = x.key.split('-')[0];
           const itemToRemove = totalKvp.filter(
             (y: any) => y.key === leftPart
           )[0];
           const indexOfItemToRemove = totalKvp.indexOf(itemToRemove);
           totalKvp.splice(indexOfItemToRemove, 1);
           totalKvp.splice(indexOfItemToRemove, 0, {
             key: x.key,
             value: x.value,
           });
           return;
         }
         totalKvp.push({ key: x.key, value: x.value });
       }
     });
   }

   return totalKvp;
 };

 useEffect(() => {
   if (value) {
     setData(value?.data ?? []);
   }
 }, [value]);
 console.log(data);<pre><pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900