javascript - 从 fetch api 返回数组到 reducer
问题描述
这有效:
const initialState = [
{ id: 1, title: 'This way', completed: true },
{ id: 3, title: 'array definition works', completed: true },
{ id: 2, title: 'As expected', completed: false },
];
硬编码数组按预期工作,但如果我尝试从异步获取 api 动态获取值,则:
“拼命尝试让它发挥作用”
(async () => {
initialState.push(getPostAsync('/posts').then(data =>[
{ id: 1, title: data.title, completed: true },
{ id: 3, title: 'This way doesnt', completed: false },
{ id: 2, title: 'work ', completed: false }]));
})();
export const filterForum = (forums, searchKeyword) => (
Object.values(forums).filter(forum => forum.title.includes(searchKeyword))
)
× TypeError:无法读取未定义的属性“包含”
解决方案
根据您的回复,forums
您传递给的变量filterForum
(我将其称为 filterForums 但 W/E)未定义。因此,当您尝试对其调用 .filter 时,它会爆炸,因为 .filter 未在未定义的变量上定义。
推荐阅读
- php - 未找到网络托管“Illuminate\Foundation\Application”上的 laravel 应用程序问题
- javascript - 我无法在仪表板中安装任何软件包,例如 react-bootstrap
- amazon-web-services - 如何连接到 AWS 中的 EC2 实例?
- azure-devops - 如何使用 Microsoft.VisualStudio.Services.WebApi 查询已完成的版本?
- firebase - React Native-如何按数字升序对平面列表中的项目进行排序?
- c# - 在 ef 核心中的列表 A 中找到不存在的项目并且在列表 B 中存在
- swift - Swift:声明 Database.database().reference() 时,ActionExtension 未打开
- r - 只保留第一次出现为真的最快方法;设置休息为假
- python - 在一维序列的大列表上的 Scipy 二次一维插值非常慢
- python - 如何使用 python-pptx 包自动从 Microsoft Forms 中提取信息?