infiniteHandler($state) {
var next = db
.collection("posts")
.orderBy("timestamp", "desc")
.startAfter(this.lastVisible)
.limit(3)
next.get().then(documentSnapshots => {
//Get the last visible document
// this.lastVisible =
// documentSnapshots.docs[documentSnapshots.docs.length - 1]
if (documentSnapshots.docs.length == 0) $state.complete()
else {
this.$store.commit(
"modules/posts/updateLastVisible",
documentSnapshots.docs[documentSnapshots.docs.length - 1].data()
.timestamp
)
}
documentSnapshots.forEach(doc => {
var post = doc.data()
post.docID = doc.id
this.$store.commit("modules/posts/pushPost", post)
})
$state.loaded()
})
}
这是我的无限加载处理程序,一旦到达列表的末尾,它将获取新的数据库条目。到目前为止工作正常。
这是我在加载页面时的第一个抓取内容
async fetch({ store }){
if (store.state.modules.posts.posts.length < 5) {
let posts = []
await db
.collection("posts")
.orderBy("timestamp", "desc")
.limit(3)
.get()
.then(querySnapshot => {
store.commit(
"modules/posts/updateLastVisible",
querySnapshot.docs[2].data().timestamp
)
querySnapshot.forEach(doc => {
var x = doc.data()
x.docID = doc.id
posts.push(x)
})
})
store.commit("modules/posts/fetchedPosts", posts)
}
}
基本上,问题是当我在无限加载处理程序中进行获取时,我再次获取了页面加载时获取的前3个条目,这导致条目被显示两次,因为{{1} }具有我在加载时获取的第3个元素的时间戳,因此应将其忽略。
在这些元素之后,this.lastVisible
都可以正常工作,但是再次加载前3个没有意义。
我用devtools检查了商店,一切正常,当第一次调用infiniteLoading Handler时,.startAfter
的值正确。
赏金编辑: 好的,所以我仍然遇到尝试尝试解决该问题的问题,但问题仍然存在……我将悬赏,希望任何人都能提供帮助。
答案 0 :(得分:0)
您实际上不需要第一次获取。挂载infiniteHandler时将单独调用它。如果没有调用,您可以尝试使用函数
this。$ refs.infiniteLoading.attemptLoad(); //'infiniteLoading'是组件的ref属性
这实际上将为您调用infiniteHandler函数。
编辑:检查功能之一当前是否正在运行。在处理程序部分
infiniteHandler($state) {
//Check if its currently loading
this.$nextTick(()=>{
if (this.isDocSnapShotLoading){
return;
}
});
//set as currently loading
this.isDocSnapShotLoading = true;
var next = db
.collection("posts")
.orderBy("timestamp", "desc")
.startAfter(this.lastVisible)
.limit(3)
next.get().then(documentSnapshots => {
//Get the last visible document
// this.lastVisible =
// documentSnapshots.docs[documentSnapshots.docs.length - 1]
if (documentSnapshots.docs.length == 0) $state.complete()
else {
this.$store.commit(
"modules/posts/updateLastVisible",
documentSnapshots.docs[documentSnapshots.docs.length - 1].data()
.timestamp
)
}
documentSnapshots.forEach(doc => {
var post = doc.data()
post.docID = doc.id
this.$store.commit("modules/posts/pushPost", post)
})
$state.loaded()
//set completed loading
this.isDocSnapShotLoading = false;
})
}
在获取部分
async fetch({ store }){
if (store.state.modules.posts.posts.length < 5) {
//check if currently loading
this.$nextTick(()=>{
if (this.isDocSnapShotLoading){
return;
}
});
//set as currently loading
this.isDocSnapShotLoading = true;
let posts = []
await db
.collection("posts")
.orderBy("timestamp", "desc")
.limit(3)
.get()
.then(querySnapshot => {
store.commit(
"modules/posts/updateLastVisible",
querySnapshot.docs[2].data().timestamp
)
querySnapshot.forEach(doc => {
var x = doc.data()
x.docID = doc.id
posts.push(x)
})
//set as completed loading.
this.isDocSnapShotLoading = false;
})
store.commit("modules/posts/fetchedPosts", posts)
}
}
答案 1 :(得分:0)
如果您想忽略infiniteHandler中的前3个帖子,则可以创建一个帖子数组,在其中存储帖子ID并检查帖子ID是否已加载。我知道应该使用查询解决此问题,但作为临时解决方案,我希望它对您有用。
infiniteHandler($state) {
var next = db
.collection("posts")
.orderBy("timestamp", "desc")
.startAfter(this.lastVisible)
.limit(3)
next.get().then(documentSnapshots => {
//Get the last visible document
// this.lastVisible =
// documentSnapshots.docs[documentSnapshots.docs.length - 1]
if (documentSnapshots.docs.length == 0) $state.complete()
else {
this.$store.commit(
"modules/posts/updateLastVisible",
documentSnapshots.docs[documentSnapshots.docs.length - 1].data()
.timestamp
)
}
documentSnapshots.forEach(doc => {
var check = this.postIdArray.indexOf(doc.id);
if(check == -1){
var post = doc.data()
post.docID = doc.id
this.$store.commit("modules/posts/pushPost", post);
this.postIdArray[] = doc.id;
}
})
$state.loaded()
})
}
async fetch({ store }){
this.postIdArray = [];
if (store.state.modules.posts.posts.length < 5) {
let posts = []
await db
.collection("posts")
.orderBy("timestamp", "desc")
.limit(3)
.get()
.then(querySnapshot => {
store.commit(
"modules/posts/updateLastVisible",
querySnapshot.docs[2].data().timestamp
)
querySnapshot.forEach(doc => {
var x = doc.data()
x.docID = doc.id
this.postIdArray[] = doc.id;
posts.push(x)
})
})
store.commit("modules/posts/fetchedPosts", posts)
}
}
答案 2 :(得分:0)
好吧,所以我找到了一个暂时的解决方案,该解决方案目前可以使用,但是仍然不够漂亮:
documentSnapshots.forEach(doc => {
if (
doc.id !== this.posts[0].docID &&
doc.id !== this.posts[1].docID &&
doc.id !== this.posts[2].docID
) {
var post = doc.data()
post.docID = doc.id
this.$store.commit("modules/posts/pushPost", post)
}
})
我也尝试通过不同的解决方案来提高效率,谢谢您的帮助。