Javascript 通过不在Firebase实时数据库中工作实现无限滚动顺序
我正在尝试用实时数据库实现无限滚动。我学习了一个教程。它以正确的顺序加载前6条记录。使用的函数Javascript 通过不在Firebase实时数据库中工作实现无限滚动顺序,javascript,firebase,firebase-realtime-database,Javascript,Firebase,Firebase Realtime Database,我正在尝试用实时数据库实现无限滚动。我学习了一个教程。它以正确的顺序加载前6条记录。使用的函数getSamples()orderby.orderByChild('age')其中age是一个数字 但当我向下滚动调用函数loadData(event)时,它甚至没有被调用 请注意,如果我在这两个函数中都将.orderByChild('age')更改为.orderByKey(),则一切正常 这是我目前的代码: home.ts import { Component, OnInit, ViewChild }
getSamples()
orderby.orderByChild('age')
其中age是一个数字
但当我向下滚动调用函数loadData(event)
时,它甚至没有被调用
请注意,如果我在这两个函数中都将.orderByChild('age')
更改为.orderByKey()
,则一切正常
这是我目前的代码:
home.ts
import { Component, OnInit, ViewChild } from '@angular/core';
import * as firebase from 'firebase';
import { AngularFireDatabase, AngularFireObject, AngularFireList } from '@angular/fire/database';
import { IonInfiniteScroll } from '@ionic/angular';
@Component({
selector: 'app-newproduct',
templateUrl: './newproduct.page.html',
styleUrls: ['./newproduct.page.scss'],
})
export class NewproductPage implements OnInit {
@ViewChild(IonInfiniteScroll, { static: false}) infiniteScroll: IonInfiniteScroll;
bookingListRef: AngularFireList<any>;
lastkey: string ="";
samples=[];
isFinished = false;
constructor(private db: AngularFireDatabase) { }
ngOnInit() {
this.getSamples();
}
getSamples(){
firebase.database().ref("users/")
.orderByChild('age')
.limitToFirst(6)
.once("value", snap=> {
snap.forEach(child => {
// store last key
this.lastkey = child.key;
// push data sample array
this.samples.push(child.val());
})
})
}
loadData(event){
console.log(this.lastkey);
firebase.database().ref("users/")
.orderByChild('age')
// start at the last key we get
.startAt(this.lastkey).limitToFirst(3).once("value", snap=>{
// hide the spinner
event.target.complete();
// if no. of children is one, data is loaded fully
if(snap.numChildren()==1){
console.log("in here");
this.infiniteScroll.disabled = true;
this.isFinished = true;
}
else {
console.log("in here");
snap.forEach( child=> {
if(this.lastkey != child.key){
this.lastkey=child.key;
this.samples.push(child.val());
}
})
}
})
}
}
console.log(this.lastkey)
返回正确的索引,转到写入消息的条件console.log(“在此处”)代码>但加载数据
函数不返回下一条记录
编辑
我当前的加载数据代码。问题:总是加载相同的下6条记录。
看
为了能够startAt
正确的节点,您需要(最多)锚节点(您开始的节点)的两件事情:
- 对其进行排序的属性的值。在你的例子中,这就是age属性的值
- 节点的键,用于在多个节点具有相同值时消除歧义。如果值是唯一的,则不需要此键
您只传递密钥,这不是一个选项。您必须同时保留值和键
因此:
然后:
firebase.database().ref("users/")
.orderByChild('age')
.startAt(this.lastAge, this.lastkey).limitToFirst(3).once("value", snap=>{
...
需要添加this.lastAge=child.val().age代码>在else中,其余都正常
loadData(event){
console.log(this.lastkey);
firebase.database().ref("users")
.orderByChild('age')
//.orderByKey()
// start at the last key we get
.startAt(this.lastAge,this.lastkey)
.limitToFirst(6)
.once("value", snap=>{
// hide the spinner
event.target.complete();
// if no. of children is one, data is loaded fully
if(snap.numChildren()==1){
console.log("in here");
this.infiniteScroll.disabled = true;
this.isFinished = true;
}
else {
console.log("in here 2");
snap.forEach(child=> {
if(this.lastkey != child.key){
this.lastkey=child.key;
this.samples.push(child.val());
console.log(this.samples);
}
})
}
})
}
firebase.database().ref("users/")
.orderByChild('age')
.limitToFirst(6)
.once("value", snap=> {
snap.forEach(child => {
this.lastAge = child.val().age;
this.lastkey = child.key;
this.samples.push(child.val());
})
})
firebase.database().ref("users/")
.orderByChild('age')
.startAt(this.lastAge, this.lastkey).limitToFirst(3).once("value", snap=>{
...