I'm learning React and I'm trying to render 10 pieces of data from a specific API. This is a function I wrote for iterating through the fetched data and taking the title and the image:
for (let i = 0; i < 10; i++) { data.push({ title: someMethod(allData)[i].children[0].data, image: someMethod(allData)[i].attribs.src, }); }
I don't know why but one of the images gives me that error:
index.js:1 TypeError: Cannot read property 'attribs' of undefined
Which stops the whole rendering. I wanted to try to put a temporary placeholder image so the rest of the images can be loaded without any errors.
I thought about adding conditions into the image line inside the loop but it didn't work for me. What's the right way to do that?
If I wasn't clear enough please comment what wasn't clear and I'll try to describe it better.
https://stackoverflow.com/questions/65895318/how-to-set-default-value-in-case-of-error January 26, 2021 at 10:58AM
没有评论:
发表评论