javascript - 有没有更好的方法来获取 Firebase 文档参考?
问题描述
我正在为我的 Firebase 应用程序构建分页。我正在使用 Angular 和 AngularFire 库。我正在尝试获取DocumentReference用于分页,但我的代码似乎有点奇怪。也许您知道获取文档参考的更好解决方案?
在我的组件中:
posts: any;
lastPost: any;
constructor(private afs: AngularFirestore) {
afs.collection('posts', ref => ref.orderBy('createdAt', 'desc').limit(10)).snapshotChanges().pipe(take(1)).subscribe(actionArray => {
this.posts = actionArray.map(item => {
return {
...item.payload.doc.data()
}
})
this.lastPost = actionArray[actionArray.length-1].payload.doc; // get the docReference
});
}
稍后在组件中,我有另一个函数,我传递此文档引用以以此文档引用为起点加载其他帖子:
loadOtherPost() {
this.firestore.collection('posts', ref => ref.orderBy('createdAt', 'desc').startAfter(this.lastPost).limit(10)).snapshotChanges().pipe(take(1)).subscribe(actionArray => {
this.posts = actionArray.map(item => {
return {
...item.payload.doc.data()
}
})
})
}
在视图中:
显示新消息☀我的问题:
- 有更好的方法吗?我看到很多代码重复。
- 我需要处理取消订阅事件以避免性能下降吗?
- 是否可以使用类似
.push()
post 属性的东西?
loadOtherPost() {
this.firestore.collection('posts', ref => ref.orderBy('createdAt', 'desc').startAfter(this.lastPost).limit(10)).snapshotChanges().pipe(take(1)).subscribe(actionArray => {
this.posts.push(actionArray.map(item => {
return {
...item.payload.doc.data()
}
}))
})
}
解决方案
- ?
- ?
- 是的,我发现这是可能的!像这样:
this.notes.push.apply(this.notes, newNotes);
推荐阅读
- c# - Visual Studio 2017 - 诊断工具 - 堆分析影响程序内存消耗
- postgresql - PostgreSQL 中最早和最早的时间
- excel - Excel 宏 - 将数据从一张表传输到另一张表时发送自动电子邮件(复制整行)
- python - 即使在通过 Selenium Python 升级到 v0.23 之后,Geckodriver 仍试图访问旧版本 v0.19
- javascript - 当我只想为一个输入字段显示错误气泡时,如何正确使用 reportValidity()?
- java - 使用 Prometheus 检测 Java 应用程序
- spring-boot - Spring Boot YAML 绑定:绑定属性失败
- sql - 使用 CASE 语句时的 Oracle SQLPLUS AVG 函数
- python - 从 for 循环输出 Python 连接大数据帧
- c - 如何从c中的文件中获取元数据