I would like to read the preferred_part and if it is true it should display checkmark and if it is false it won't display anything. This is the code I have below and the error I get is:
TypeError: Cannot read property 'substring' of undefined.
I don't even properly know how I should be able to read the boolean values of preferred part in any other way. Any sort of help will be appreciated. Please tell me how I can solve this issue. I am new to react. Thank you!
async function getParts() {
console.log("Getting parts...");
const res = await fetch("http://localhost:5000/lookup");
const data = await res.json();
setParts(data);
}
useEffect(() => {
getParts();
}, []);
function formatLightState() {
if (parts.preferred_parts = true) {
return parts.tps_part_number + " - " + parts.manufacturer + " (" + parts.technical.substring(0, 95) + " ✔" + ")"
}
else {
return parts.tps_part_number + " - " + parts.manufacturer + " (" + parts.technical.substring(0, 95) + ")"
}
}
const partsList = parts.map((option) => ({
manufacturer: option.manufacturer,
technical: option.technical,
tps_part_number: option.tps_part_number,
value: option.part_key,
quote_price: option.quote_price,
vendor_name: option.vendor_name,
vendor_id: option.vendor_key,
label: formatLightState(),
}));