Optional chaining in SvelteKit +page.svelte

63 views Asked by At

I have a line:

<img src="http://localhost:1337{data?.post?.attributes?.image?.data?.attributes?.url}" class="rounded-md" alt="">

Data coming from a Strapi API call and image is optional, so at this post it does not exists.

Even if I have used optional chaining to avoid getting HTTP 500 I still have it using Node v21 (updated from v18 to see its still presist)

TypeError: Cannot read properties of null (reading 'attributes')

Any idea how to overcome on this? For sure I have to check if post has an image and display it based on this, but I still should be able to use optional chaining to avoid getting this error or am I wrong?

Thank you!

0

There are 0 answers