angular - 离子:Firebase 查询返回丢失的数据
问题描述
我正在尝试在 ıonic 中为 Infinite Scroll 设置分页系统。我想要实现的是获取特定数量的数据块(例如 10 个数据)。然后,当用户到达底部时,它应该从它记录的最后一个数据开始获取另一个数据块。但是,当我运行我的代码时,查询给出了一个包含 1 个数据而不是 10 个数据的块的结果。
我尝试添加控制台日志并尝试了解问题。我分享了我掌握的信息。
feed.page.ts
export class FeedPage implements OnInit {
ideas: any;
offset =5;
nextKey: any;
prevKeys: any[] = [];
subscription: any;
data: any[] = [];
constructor(private afstore:AngularFirestore,
private feedsrvc:FeedService) { }
ngOnInit() {
this.getIdeas("5dd46790-b369-11e9-bd2b-a7c5019a82a5")
console.log("baş")
console.log(this.data)
}
loadData(event) {
setTimeout(() => {
console.log('Done');
console.log('Bak bu: '+this.nextKey);
this.getIdeas(this.nextKey);
event.target.complete();
}, 500);
}
private getIdeas(key?){
this.subscription = this.feedsrvc.getIdeas(this.offset,key).valueChanges().subscribe(ideas =>{
console.log("Adet: "+this.offset)
console.log("Önce")
console.log(ideas)
//this.ideas = ideas.slice(0,this.offset)
this.ideas = ideas
this.data=this.data.concat(this.ideas)
//console.log("ud"+this.ideas)
this.nextKey = `${ideas[this.offset].uuid}`})
console.log("Son Key: "+this.nextKey)
}
}
feed.service.ts
export class FeedService {
constructor(private afstore:AngularFirestore){}
getIdeas(offset, startKey?){
return this.afstore.collection('posts', ref => ref.limit(offset+1).orderBy("uuid").startAt(startKey))
}
}
解决方案
我认为您的查询顺序很重要。
你正在收集{pagesize} + 1
,然后开始{startkey}
,我假设是在结束之前的一个。
尝试这个:
return this.afstore.collection('posts', ref => ref.startAt(startKey).orderBy("uuid").limit(offset+1))
顺便说一句,分页不太适合 Firebase,但是当我不久前尝试使用它时,我设法使用此答案中的代码获得了一个不错的“加载更多”按钮。它可能会帮助你。
推荐阅读
- javascript - 如何使用样式组件键入 Ant 设计?
- python - 使用方法在python中创建一个类。下面的描述
- computer-vision - 数字输出的损失函数和最后一层
- c++ - 用字符串向量的位置初始化字符串
- python - 错误:没有这样的文件 --> 执行程序
- php - 简单的网络问卷
- javascript - Firebase 函数 - 随机选择 2 个子节点并将它们放置在其他节点中
- caching - 使用有效的缓存策略服务静态资产 - Google Analytics
- .net - Visual Studio 构建成功但 dotnet 构建 cli 失败
- r - 代码块如何使用 knit_expand 循环绘制结果与标题?