我只是在做练习,而不是JS专家,所以我以Axios
作为主题的一部分,尝试在JS上进行MVC模式。我已经被这个错误困扰了几个小时了。
我试图用try/catch
包围它,但是不起作用。
无数次了,我可以从服务器获取结果。但是现在我不能了。它也可以在分页部分从此服务器-Food2Fork API获取结果。
SearchView.js
For pagination
export const renderResults = (recipes, page = 1, resultPerPage = 10) => {
//we start at 0 ,
const start = (page - 1) * resultPerPage; // 0 - 1 = 1 * 10 = 10
//end
const end = page * resultPerPage;
//params of slice (start , end )
recipes.slice(start, end).forEach(renderRecipe);
//Render the btn to the page
renderButtons(page, recipes.length, resultPerPage); }
通过我的模型 Search.js
export default class Search {
//Constructor
constructor(query){
this.query = query;
}
//async method
async getResultFood() {
try{
const resultRequest = await axios(proxy+searchURL+keyAPI+`&q=${this.query}`);
this.result = resultRequest.data.recipes;
}catch(e){
console.log(e);
}
} }
从我的控制器 index.js
const controlSearch = async () => {
//1 get the search query from the view - html form
const query = SearchView.getFormTextSearch(); //from SearchView.js
console.log(query);
if(query) {
//2 new search object and add to state
state.search = new Search(query);
//3 prepare the UI for results
SearchView.clearSearchField();
SearchView.clearSearchResults();
//Call loader to the container
renderLoader(variables.resultContainer);
//4 Search for recipes
try{
await state.search.getResultFood();
//5 Render results - the `result` is the name of var
//console.log(state.search.result)
SearchView.renderResults(state.search.result);
//Dismiss the loader
dismissLoader();
}catch(error){
console.log(error);
}
} }
我错过了什么?
编辑:
&q=${this.query}
);
Log Result:
Log Result:
未定义