faxunil's avatar

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'links')

Dear All, I would like to create a pager for my fetcher data. (I'm using Vue 3) The data is comes from Laravel API - paginated data. I have data, links, and meta data. I would like to pass over to my pager component the meta and links data to render the pager.

<Pagination :links="posts.links" :meta="posts.meta"></Pagination>

In the pagination vue file:

    name: "Pagination",
    props: ['meta', 'links'],
    mounted() {

    },
    data() {
        return {
        }
    },
    components: {
        ChevronLeftIcon,
        ChevronRightIcon,
    },
}

In the template I'm checking the variable: 
```<template>
    {{ links }} ... ```
It gives back the data correctly.

Bit I also have a consol error:
```app.js?time=6150567959fd8:80040 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'links')
    at Proxy.render (app.js?time=6150567959fd8:80040)
    at renderComponentRoot (app.js?time=6150567959fd8:62984)
    at ReactiveEffect.componentUpdateFn [as fn] (app.js?time=6150567959fd8:66797)
    at ReactiveEffect.run (app.js?time=6150567959fd8:61397)
    at callWithErrorHandling (app.js?time=6150567959fd8:69180)
    at flushJobs (app.js?time=6150567959fd8:69416)
    at flushJobs (app.js?time=6150567959fd8:69431)

Is this the proper way to avoid that?

<Pagination :links="posts?.links" :meta="posts?.meta"></Pagination>

With ?.links

Thanks Gergely

0 likes
2 replies
piljac1's avatar
piljac1
Best Answer
Level 28

If your posts property is filled by an API call (which it most likely is), it makes sense to do so because your posts value is probably undefined (I'm guessing it is the underlying property of a parent object) initially. An alternative would be to set a default prop value which would be an empty object.

Please or to participate in this conversation.