无法到达对象 AngularFire2/Typescript 的 属性
Cannot reach property of object AngularFire2/Typescript
我在我的项目中添加了一个搜索栏,并从 ionic 文档中获取了代码
getItems(ev: any) {
this.initializeItems();
const val = ev.target.value;
if (val && val.trim() != '') {
this.items = this.items.pipe(filter(item => {
console.log(item);
let t = item.title;
return (t.toLowerCase().indexOf(val.toLowerCase()) > -1);
}))
}
}
这就是我初始化项目的方式
initializeItems(){
this.items = this.newslist;
}
并且在构造函数中
this.newslist = afDB.list('information/news', (ref) => ref.orderByChild('inverteddatetime')).snapshotChanges().pipe(
map(actions =>
actions.map(a => ({ key: a.key, ...a.payload.val() }))
)
);
我遇到的错误是 let t = item.title;
,其中 "title" 带有红色下划线,消息
Property "title" does not exist on type {}
当我在 console.log(item);
中记录项目时,我得到了正确的结果,尽管它们具有适当的属性:
(2) [{…}, {…}]
0:{
datetime:"Saturday, June 23, 2018 12:28 AM"
image:"data:image/jpeg;base64,/9j/4..."
inverteddatetime:-1529703181246
key:"-LFdPXgxgAQA1RVnGmMD"
subtitle:"Water can cause problems, be wary of the things."
text:"Water quality is a subject that’s been big news lately. Residents of Flint, Michigan are suffering from toxic levels of lead in ..."
title:"12 Toxins in Your Drinking Water"
}
1 : {key: "-LFXmks2UICxqTNkicNv", datetime: "Thursday, June 21, 2018 5:35 PM", subtitle: "Unfortunate problems", …} length : 2
__proto__ : Array(0)
数据结构:
Firebase Data Structure(我没有足够的声誉在线程上显示它)
我有一个简单的问题,我不明白为什么我无法到达项目的 属性 "title",尽管当我登录它时它显示了所有项目及其属性。是因为它正在返回 JSON 并且我应该将它映射到一个数组吗?
这是类型错误,因为打字稿不“知道”您的对象(在本例中为“item”)数据形状。
通常你会想输入它然后解析它,像这样:
let t = (item as any).title
但在您的情况下,您似乎正在尝试以类似的方式应用过滤器:
您的解决方案应该 1:1 类似:将 .map 应用到您拥有的可观察对象上(this.items.map(等))
像这样:
getItems(ev: any) {
this.initializeItems();
const val = ev.target.value;
if (val && val.trim() != “”) {
this.items = this.items.map( items => items.filter( item => :::your filter condition here:::))
}
}
我在我的项目中添加了一个搜索栏,并从 ionic 文档中获取了代码
getItems(ev: any) {
this.initializeItems();
const val = ev.target.value;
if (val && val.trim() != '') {
this.items = this.items.pipe(filter(item => {
console.log(item);
let t = item.title;
return (t.toLowerCase().indexOf(val.toLowerCase()) > -1);
}))
}
}
这就是我初始化项目的方式
initializeItems(){
this.items = this.newslist;
}
并且在构造函数中
this.newslist = afDB.list('information/news', (ref) => ref.orderByChild('inverteddatetime')).snapshotChanges().pipe(
map(actions =>
actions.map(a => ({ key: a.key, ...a.payload.val() }))
)
);
我遇到的错误是 let t = item.title;
,其中 "title" 带有红色下划线,消息
Property "title" does not exist on type {}
当我在 console.log(item);
中记录项目时,我得到了正确的结果,尽管它们具有适当的属性:
(2) [{…}, {…}]
0:{
datetime:"Saturday, June 23, 2018 12:28 AM"
image:"data:image/jpeg;base64,/9j/4..."
inverteddatetime:-1529703181246
key:"-LFdPXgxgAQA1RVnGmMD"
subtitle:"Water can cause problems, be wary of the things."
text:"Water quality is a subject that’s been big news lately. Residents of Flint, Michigan are suffering from toxic levels of lead in ..."
title:"12 Toxins in Your Drinking Water"
}
1 : {key: "-LFXmks2UICxqTNkicNv", datetime: "Thursday, June 21, 2018 5:35 PM", subtitle: "Unfortunate problems", …} length : 2
__proto__ : Array(0)
数据结构: Firebase Data Structure(我没有足够的声誉在线程上显示它)
我有一个简单的问题,我不明白为什么我无法到达项目的 属性 "title",尽管当我登录它时它显示了所有项目及其属性。是因为它正在返回 JSON 并且我应该将它映射到一个数组吗?
这是类型错误,因为打字稿不“知道”您的对象(在本例中为“item”)数据形状。 通常你会想输入它然后解析它,像这样:
let t = (item as any).title
但在您的情况下,您似乎正在尝试以类似的方式应用过滤器:
您的解决方案应该 1:1 类似:将 .map 应用到您拥有的可观察对象上(this.items.map(等))
像这样:
getItems(ev: any) {
this.initializeItems();
const val = ev.target.value;
if (val && val.trim() != “”) {
this.items = this.items.map( items => items.filter( item => :::your filter condition here:::))
}
}