Angularefire2 - 查询未返回预期响应
问题描述:
我试图从具有一组电子邮件地址的firebase中获取列表。我只想要与查询中的电子邮件相匹配的项目。这是结构。我试图获取匹配的项目[email protected]
下collaborators
Angularefire2 - 查询未返回预期响应
查询:
this.firebase.list('/todo', {
query: {
orderByChild: 'collaborators',
equalTo: '[email protected]'
}
}).subscribe((data)=>{
console.log(data);
});
这回空数组这应该列出一个项目!
如何解决这个..
解决方案:
let results = this.firebase.list('/todo')
.map(data => data.filter((e) => {
return e.collaborators.indexOf(email) > -1;
}
));
return results
答
你想,如果它是的collaborators
一个值来访问电子邮件地址。实际上,电子邮件地址是其子项之一。
我建议重构你的模型,使用电子邮件作为关键而不是价值;例如,[email protected]: true
。我提到了它的一些优点,并提供了一些链接,涵盖了我在下面的评论中的主题。
这个小改动,你的模型后,使用map
操作映射到collaborators
和使用filter
运营商过滤掉的对象,其collaborators
对象包含其关键比赛email
的属性。
getNotesFromSpecifiedUser(email: string): Observable<any> {
let results = this.af.database.list('/todo')
.map(data => data.filter(data => (email in data.collaborators)))
return results
}
重要:如果在collaborators
没有条目的错误将被抛出,因为in
关键字不能处理空。如果可以想象collaborators
将是空的,请告诉我。
我得到这个错误'this.firebase.list(...)。map不是函数' –
您需要导入运算符。 'import'rxjs/add/operator/map'' –
这将返回一个空数组 –