简体   繁体   English

TypeError:无法读取未定义 React 的属性“forEach”

[英]TypeError: Cannot read property 'forEach' of undefined React

I am trying to fetch data from MDB database to display movies.我正在尝试从 MDB 数据库中获取数据以显示电影。 Initially I have implemented a click event to call data and no errors were found.最初我已经实现了一个点击事件来调用数据并且没有发现错误。 Until this happened!直到这件事发生!

在此处输入图像描述

And this is code:这是代码:

this.performSearch()
    }
    performSearch() {
        console.log("Perform search using moviedb")
        const urlString = "https://api.themoviedb.org/3/search/movie?query=avengers&api_key=1b5adf76a72a13bad99b8fc0c68cb085"
        $.ajax({urlString,
            success: (searchResults) => {
            console.log("Fetched data successfully")
                // console.log(searchResults)
                const results = searchResults.results

                var movieRows = []

                results.forEach((movie) => {
                  console.log(movie.title)

                });
                  const movie = <MovieRow movie={movie}/>
                  movieRows.push(movie)

                this.setState({rows: movieRows})   

You have no results.你没有结果。 To protect against this you can change为了防止这种情况,您可以更改

const results = searchResults.results

to

const results = searchResults.results ? searchResults.results : []

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应表获取问题类型错误:无法读取未定义的属性“forEach” - React table getting issue TypeError: Cannot read property 'forEach' of undefined react-table 的问题:TypeError:无法读取未定义的属性“forEach” - Issue with react-table: TypeError: Cannot read property 'forEach' of undefined 错误:TypeError:无法读取未定义的属性“forEach” - Error : TypeError: Cannot read property 'forEach' of undefined 类型错误:无法读取未定义的属性“forEach” - TypeError: Cannot read property 'forEach' of undefined UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“forEach” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined 类型错误:无法读取未定义 Javascript 的属性“forEach” - TypeError: Cannot read property 'forEach' of undefined Javascript 类型错误:无法读取未定义的属性“forEach”,javascript - TypeError: Cannot read property 'forEach' of undefined , javascript 无法读取 React 中未定义的属性“forEach” - Cannot read property 'forEach' of undefined in React 未捕获的TypeError:无法在javascript / React-router / React中读取未定义的属性&#39;forEach&#39; - Uncaught TypeError: Cannot read property 'forEach' of undefined in javascript / React-router / React React-Table 和 React-Query 获取 TypeError:无法读取未定义的属性“forEach” - React-Table & React-Query getting TypeError: Cannot read property 'forEach' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM