gpt4 book ai didi

javascript - Firestore .startAt 无法正常工作

转载 作者:搜寻专家 更新时间:2023-10-30 22:13:39 26 4
gpt4 key购买 nike

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 个条目,这导致条目显示两次,这不应该发生,因为 this .lastVisible 具有我在加载时获取的第三个元素的时间戳,因此应忽略这些。

在这些元素之后,.startAfter 一切正常,但再次加载前 3 个元素毫无意义。我用 devtools 检查了商店,一切正常,this.lastVisible 在第一次调用 infiniteLoading Handler 时具有正确的值。

赏金编辑:好吧,我仍然有问题,我尝试更多地解决它以找到问题,但它仍然存在......我现在将设置赏金,我希望任何人都能提供帮助。

最佳答案

您实际上不需要第一次获取。 infiniteHandler 将在安装时自行调用。如果它不调用,那么您可以尝试使用函数

this.$refs.infiniteLoading.attemptLoad(); // 'infiniteLoading' is the component's ref property

这实际上会为您调用 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)
}
}

关于javascript - Firestore .startAt 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52707756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com