将数组返回到其他函数的问题

时间:2019-05-26 20:07:10

标签: javascript arrays api fetch

我正在使用Omdb Api制作网页当我不将评分返回给getMovieName函数时,提取似乎工作正常。评级是对象大小3的数组。似乎在控制台中显示了此内容:

**Uncaught TypeError: Cannot read property 'length' of undefined
at HTMLFormElement.getMovieName (VM2170 main.js:5)
getMovieName @ VM2170 main.js:5
VM2170 main.js:28 Fetch Error:-S TypeError: Failed to fetch*

*

    document.getElementById('MovieForm').addEventListener('submit',getMovieName);
    function getMovieName(e){
        let MovieName =  document.getElementById('MovieName').value;
        let OmdbRatings = getOmdbRating(MovieName);
        console.log(OmdbRatings.length);
        e.preventDefault();
    }
    function getOmdbRating(MovieName){
        fetch('http://www.omdbapi.com/?i=tt3896198&apikey=1c149048&t='+MovieName
        ).then(
            function(response){
                if(response.status !== 200)
                {
                    console.log('Looks Like There is a problem');
                    return ;
                }
                response.json().then(function(data){
                    //console.log(data);
                    let ratings = [];
                    ratings = data.Ratings;
                    //const rating = data.Ratings[0].Value.substring(0,data.Rating[0].Value.indexOf("/"));
                    console.log(ratings);
                    return ratings;
                    //console.log(typeof(ratings[0].Value));
                });
            }
        )
        .catch(function(err){
            console.log('Fetch Error:-S', err);
   });
}

0 个答案:

没有答案