作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从一组团队中返回选定的团队。看来我需要将快照转换为 Team 对象。我怎么做?有没有更简单的方法将快照转换为数组?我是 firebase/angular 的新手,所以希望我遗漏了一些简单的东西。
使用 Angular 5.5.2
export class Team {
internalTeamName: string;
externalTeamName: string;
creatorNumber: number;
teamAdmins: TeamMember[];
teamMembers: TeamMember[];
constructor(code ommitted)
}
服务模块
loadTeams(email){
const teamArray: Team[] = null;
this.firestore.collection('teams', ref => ref.where('teamAdmins', 'array-contains', email))
.get()
.forEach(function(childSnapshot) {
teamArray.push(childSnapshot);})
.then(
return teamArray )
.catch(
err => console.log(err)
)
上面代码的错误:“QuerySnapshot”类型的参数不可分配给“Team”类型的参数。类型“QuerySnapshot”缺少类型“Team”中的以下属性:internalTeamName、externalTeamName、creatorNumber、teamAdmins、teamMembers
调用上述函数的函数:调用服务函数时的最佳做法是什么?
public teamArray: Team[] = null;
ngOnInit() {
this.teamArray = this.teamCrudService.loadTeams(this.authService.email);
}
最佳答案
.collection().get()
不返回数组;它返回一个QuerySnapshot,它有一个property .docs
,它是一个QueryDocumentSnapshot
数组,每个数组都有一个property .data
,即从文档中读取的数据。
请看一下公共(public)文档 https://firebase.google.com/docs/reference/js/firebase.firestore.CollectionReference
关于angular - 如何将 Firebase Querysnapshot 转换为对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65298451/
我是一名优秀的程序员,十分优秀!