Firestore .startAt无法正常工作

Bad*_*dgy 5 javascript firebase vue.js google-cloud-firestore nuxt

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()
    })
}
Run Code Online (Sandbox Code Playgroud)

这是我的无限加载处理程序,它在到达列表末尾时获取新的数据库条目.到目前为止工作正常.

这是我加载页面时的第一次获取

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上问题是,当我在无限加载处理程序中获取时,我获取了我在页面加载时再次获取的前3个条目,导致条目显示两次,这不应该发生,因为它this.lastVisible具有第3个元素的时间戳我在加载时获取,所以应该忽略它们.

在这些元素之后,一切都运行良好,.startAfter但前3个再次加载没有任何意义.我用devtools检查了商店,一切正常,this.lastVisible在第一次调用infiniteLoading Handler时有正确的值.

赏金编辑:好的,所以我仍然有问题我试图用它来更多地找到问题,但它仍然存在...我现在将设置一个赏金,我希望任何人都能够提供帮助.

zap*_*ing 1

您实际上并不需要第一次获取。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;
    })
}
Run Code Online (Sandbox Code Playgroud)

在获取部分

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)
    }
}
Run Code Online (Sandbox Code Playgroud)