javascript - 根据类别过滤 Firebase Firestore 条目
问题描述
我需要根据类别过滤条目。这是我得到的错误。
TypeError:无法在 VueComponent.filteredItems 读取 null 的属性“过滤器”
Vue组件:
<v-card
v-for="product in filteredItems"
:key="product.id"
class="mx-auto"
max-width="344"
>
[...]
计算:
...mapState('products', ['products']),
filteredItems() {
let filtered = this.products
if (this.search) {
filtered = this.products.filter(
item => item.name.category.toLowerCase().indexOf(this.search.toLowerCase()) > -1
)
}
return filtered
}
},
导出默认值:
export default {
props: {
filterCategory: String
},
data: () => ({
search: 'Personal',
Firestore JSON 条目:
name: {action:"button", category: "Personal" [...]
如何修复错误?
解决方案
TypeError:无法在 VueComponent.filteredItems 读取 null 的属性“过滤器”
意思是 this.products 在某些时候为空。当您将默认状态设置为 null 然后在第一次加载时,当尚未从 firebase/api 获取数据时,它仍然为 null。所以一个简单的解决方案是
if (this.search) {
filtered = this.products && this.products.filter(
item => item.name.category.toLowerCase().indexOf(this.search.toLowerCase()) > -1
)
}
请检查此代码。
推荐阅读
- c# - Excel 互操作:获取用户设置的过滤器,并在经过一些处理后将其设置回来
- python - 如何使用 Python 将 .png 标签转换为 EPL?
- python - 列表索引在 Jinja 中的工作原理
- python-3.x - 尝试在 python 中连接 mongodb 数据库时出现配置错误
- laravel - 如何在 Laravel Livewire 的引导模式中显示帖子记录?
- docker - Cairo docker build 由于缺少 pixman 依赖项而失败
- api - 将数据从 RDBMS 迁移到 Arango DB:批量上传
- java - PostgreSQL 的批量更新插入最佳实践
- c - 我无法为单链表运行快速排序。m 无法将递归调用的条件放入快速排序函数中
- workbox - workbox.cacheableResponse.Plugin 不抛出构造函数错误,v6.0.2