切片”"/>
错误问题无法读取未定义的属性“切片”
我收到这个错误
TypeError: Cannot read property 'slice' of undefined
at Object.renderResults (searchView.js:93)
at _callee$ (index.js:85)
at tryCatch (runtime.js:65)
at Generator.invoke [as _invoke] (runtime.js:303)
at Generator.prototype.(:8081/anonymous function) [as next] (webpack:///./node_modules/babel-polyfill/node_modules/regenerator-runtime/runtime.js?:117:21)
at step (index.js:41)
at eval (index.js:41)
在我的项目的搜索栏中搜索“披萨”之后,这是我的 github 存储库:
我相信这部分代码可能有错误:
export const renderResults = (recipes, page = 1, resPerPage = 10) => {
// render results of currente page
const start = (page - 1) * resPerPage;
const end = page * resPerPage;
recipes.slice(start, end).forEach(recipe => renderRecipe(recipe));
// render pagination buttons
renderButtons(page, recipes.length, resPerPage);
};
在文件 searchView.js 中:
import { elements } from './base';
export const getInput = () => elements.searchInput.value;
export const clearInput = () => {
elements.searchInput.value = '';
};
export const clearResults = () => {
elements.searchResList.innerHTML = '';
elements.searchResPages.innerHTML = '';
};
export const highlightSelected = id => {
const resultsArr = Array.from(document.querySelectorAll('.results__link'));
resultsArr.forEach(el => {
el.classList.remove('results__link--active');
});
document.querySelector(`.results__link[href*="${id}"]`).classList.add('results__link--active');
};
/*
// 'Pasta with tomato and spinach'
acc: 0 / acc + cur.length = 5 / newTitle = ['Pasta']
acc: 5 / acc + cur.length = 9 / newTitle = ['Pasta', 'with']
acc: 9 / acc + cur.length = 15 / newTitle = ['Pasta', 'with', 'tomato']
acc: 15 / acc + cur.length = 18 / newTitle = ['Pasta', 'with', 'tomato']
acc: 18 / acc + cur.length = 24 / newTitle = ['Pasta', 'with', 'tomato']
*/
export const limitRecipeTitle = (title, limit = 17) => {
const newTitle = [];
if (title.length > limit) {
title.split(' ').reduce((acc, cur) => {
if (acc + cur.length <= limit) {
newTitle.push(cur);
}
return acc + cur.length;
}, 0);
// return the result
return `${newTitle.join(' ')} ...`;
}
return title;
}
const renderRecipe = recipe => {
const markup = `
<li>
<a class="results__link" href="#${recipe.recipe_id}">
<figure class="results__fig">
<img src="${recipe.image_url}" alt="${recipe.title}">
</figure>
<div class="results__data">
<h4 class="results__name">${limitRecipeTitle(recipe.title)}</h4>
<p class="results__author">${recipe.publisher}</p>
</div>
</a>
</li>
`;
elements.searchResList.insertAdjacentHTML('beforeend', markup);
};
// type: 'prev' or 'next'
const createButton = (page, type) => `
<button class="btn-inline results__btn--${type}" data-goto=${type === 'prev' ? page - 1 : page + 1}>
<span>Page ${type === 'prev' ? page - 1 : page + 1}</span>
<svg class="search__icon">
<use href="img/icons.svg#icon-triangle-${type === 'prev' ? 'left' : 'right'}"></use>
</svg>
</button>
`;
const renderButtons = (page, numResults, resPerPage) => {
const pages = Math.ceil(numResults / resPerPage);
let button;
if (page === 1 && pages > 1) {
// Only button to go to next page
button = createButton(page, 'next');
} else if (page < pages) {
// Both buttons
button = `
${createButton(page, 'prev')}
${createButton(page, 'next')}
`;
} else if (page === pages && pages > 1) {
// Only button to go to prev page
button = createButton(page, 'prev');
}
elements.searchResPages.insertAdjacentHTML('afterbegin', button);
};
export const renderResults = (recipes, page = 1, resPerPage = 10) => {
// render results of currente page
const start = (page - 1) * resPerPage;
const end = page * resPerPage;
recipes.slice(start, end).forEach(recipe => renderRecipe(recipe));
// render pagination buttons
renderButtons(page, recipes.length, resPerPage);
};
回答如下:
我也遇到了同样的错误。 收到此错误后,我知道当数组或您使用
slice()
未定义或 null 的任何内容时。
这就是发生此错误的原因。
response(dd.slice(0, 20));
错误
> Index:103 Uncaught TypeError: Cannot read property 'slice' of
> undefined
> at Object.success (Index:103)
> at fire (jquery-1.12.4.js:3232)
> at Object.fireWith [as resolveWith] (jquery-1.12.4.js:3362)
> at done (jquery-1.12.4.js:9840)
> at XMLHttpRequest.callback (jquery-1.12.4.js:10311)
更多推荐
错误问题无法读取未定义的属性“切片”
本文发布于:2024-05-13 16:54:18,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
- 上一篇: 通过 nodemailer 发送的电子邮件进入 gmail 的垃圾邮件
- 下一篇: 3085 蚱蜢
发布评论