函数内部调用的函数出现错误 "not defined"
Getting error "not defined" for function called inside function
我有一个 class,在那个 class 中,它有两个函数和一个静态函数。在名为 requestMovie()
的第一个函数中,我想从 API 中获取数据,然后第二个函数用于生成名为 generateUrl()
的 URL,在第三个函数中名为 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;
并且出现此错误说:
enter image description here
首先 requestMovies
是一个实例方法,它应该像 this.requestMovies()
一样被引用,其次,因为这是一个实例方法,所以它不能从静态方法中调用,所以方法 searchMovie
不能是静态方法。
我有一个 class,在那个 class 中,它有两个函数和一个静态函数。在名为 requestMovie()
的第一个函数中,我想从 API 中获取数据,然后第二个函数用于生成名为 generateUrl()
的 URL,在第三个函数中名为 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;
并且出现此错误说: enter image description here
首先 requestMovies
是一个实例方法,它应该像 this.requestMovies()
一样被引用,其次,因为这是一个实例方法,所以它不能从静态方法中调用,所以方法 searchMovie
不能是静态方法。