Uncaught (в обещании) TypeError: Невозможно прочитать свойство 'slice' из неопределенного - PullRequest
0 голосов
/ 27 сентября 2018

Я выполнял упражнение, а не тот эксперт в JS, и использовал I Axios как часть темы и пытался создать шаблон MVC для JS.Я застрял с этой ошибкой в ​​течение нескольких часов.

Я пытался окружить его 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);
    }
} }

Что я пропустил?

РЕДАКТИРОВАТЬ:

  1. const resultRequest =ожидайте axios (прокси + searchURL + keyAPI + &q=${this.query});Log Result:

log result

this.result = resultRequest.data.recipes;

Log Result: undefined

...