未捕获(在承诺中)类型错误。无法读取未定义的属性 "slice"。

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

我当时做了一个练习,对JS不是那么专业,用了I 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);
    }
} }

我错过了什么?

EDIT:

  1. const resultRequest = await axios(proxy+searchURL+keyAPI+)&q=${this.query});Log Result:

log result

  1. this.result = resultRequest.data.recipes;

Log Result:未定义

javascript axios es6-promise
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.