Search code examples
reactjsvideo-reactjs

React js shows error while showing object


I got an error when i go to console.log this object...in reactjs

Json object {"title":"Rtv","length":-1,"params":{"tvg-logo":"https://upload.wikimedia.org/wikipedia/en/thumb/4/41/Rtv_bangladesh.PNG/120px-Rtv_bangladesh.PNG","group-title":"America"},"file":"https://example.m3u8"}

Error:

TypeError: Cannot read property 'tvg-logo' of undefined

(anonymous function)

src/App.js:88

85 | 86 | return ( 87 | items.map((data,key)=>{

88 | console.log(data['params']['tvg-logo']) | ^ 89 | const {title,params} = data; 90 | return( 91 | View compiled


Solution

  • It is difficult to see why the error is occurring in the small piece of code you have provided but to avoid this error you can use the conditional chaining discussed above; see example below.

    console.log(data?.params?.tvg-logo)
    

    This will return nothing if it fails anywhere along the object path.