在函数内调用的函数出现 "未定义 "错误。

问题描述 投票:0回答:1

我有一个类,在这个类中,它有两个函数和一个静态函数。在第一个函数中,叫做 requestMovie() 我想从API中获取数据,那么第二个函数是用来生成URL的,叫做 generateUrl()在第三个函数中,称为 searchMovie() 我想叫 generateUrl()requestMovie() 函数,但出现了一个错误,说该函数没有定义

const MOVIE_ENDPOINT = 'https://api.themoviedb.org/3/';
const API_KEY = '####';


class ApiTransactions {

    requestMovies(url, onComplete, onError){
        return fetch(url)
        .then((response) => response.json())
        .then(onComplete)
        .catch(onError);
    }

    generateUrl(path){
        const url = `${MOVIE_ENDPOINT}${path}?api_key=${API_KEY}`;
        return url;
    }

    static searchMovie(keyword){
        const url = generateUrl('search/movie') + '&query=' + keyword;
        requestMovies(url,renderResult, fallbackResult);
    }
}

export default ApiTransactions;

并出现这个错误说。在这里输入图片描述

javascript function web webpack babel
1个回答
0
投票

那么首先 requestMovies 是一个实例方法,它的引用方式应该是 this.requestMovies(),其次,由于这是一个实例方法,它不能从静态方法中调用,所以方法 searchMovie 不能是静态方法。

© www.soinside.com 2019 - 2024. All rights reserved.