Answer the question
In order to leave comments, you need to log in
Why is undefined flying into the function parameter of a Vue 3 component?
Good afternoon. I have a Vue component. We get an array of data from the server into the certificate variable. The received certain fields of the certificate must be converted (only numbers should be left in the lines). The shortingTechItems function is written for this. When I throw the certificate.pto value into it, it gives the error "Cannot read properties of undefined (reading 'map')", i.e. in the function itself, undefined arrives in item. In another similar component, the same function works without errors (maybe it's the amount of data received from the API, in another component there are fewer of them, so they load faster). What is the problem and how to fix it?
<td class="border w-50 px-2 text-center">
{{ shortingTechItems(certificate.pto) }}
</td>
export default {
setup() {
const route = useRoute();
const store = useStore();
const certificate = ref({});
onMounted(async () => {
certificate.value = await store.dispatch(
"certificate/loadById",
route.params.id
);
});
const shortingTechItems = (item) => {
const arr = [];
item.map((i) => {
arr.push(Number(i.replace(/\D+/g, "")));
});
return arr;
};
return {
certificate,
shortingTechItems,
};
},
};
Answer the question
In order to leave comments, you need to log in
add a check for existence
since the method is called before the data is loaded
<td v-if="certificate.pto" class="border w-50 px-2 text-center">
{{ shortingTechItems(certificate.pto) }}
</td>
const certificate = ref({});
shortingTechItems(certificate.pto)
arrives undefined
What is the problem
how to fix?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question