简体   繁体   English

如何将我的帖子从 firestore 分页到我的 Javascript Web 应用程序。 除了 Node.js 服务器之外,我没有使用任何框架

[英]How do I paginate my posts from firestore to my Javascript web app. Am not using any framework except Node.js server

Below is the code I have tried.Am really new to programming下面是我试过的代码。我真的是编程新手

var records_per_page = 4;
  
  var first = firebase.firestore().collection("Posts").limit(records_per_page);

  first.get().then((snapshot)=>
    {
        document.getElementById("post-container").innerHTML = '';
        snapshot.forEach(function (taskValue) {
            if(snapshot.size >= 1){
                showPosts(taskValue)
                 
                    $("#next").click(function(){
                        var lastVisible = "";
                        lastVisible = snapshot.docs[snapshot.docs.length-1];
                        showMorePosts(records_per_page,lastVisible)
                    })
             
            }
           
        });
        
    }
  );

But the code fetches the first and second four documents only.但该代码仅获取前四个文档和第二个文档。 I have created a gist here to show both methods showMorePosts(records_per_page,lastVisible) and showPosts(records_per_page).我在这里创建了一个要点来展示 showMorePosts(records_per_page,lastVisible) 和 showPosts(records_per_page) 两种方法。 Stackoverflow doesn't allow me to add a lot of code. Stackoverflow 不允许我添加大量代码。

这是我的数据库的样子

You can use offset to load the page您可以使用offset加载页面

const records_per_page = 4;
const page = 2;
  
const first = firebase.firestore().collection("Posts")
                      .limit(records_per_page)
                      .offset((page - 1) * records_per_page);

first.get().then(snapshot => {
  document.getElementById("post-container").innerHTML = '';
  snapshot.forEach(taskValue => {
    // You don't need this if condition. It's impossible that this condition evaluates to false
    // if (snapshot.size >= 1) {
      showPosts(taskValue);

      $("#next").click(() => {
        const lastVisible = "";
        lastVisible = snapshot.docs[snapshot.docs.length-1];
        showMorePosts(records_per_page,lastVisible);
      });
    // }
  });
});

As mentioned by @jabaa, use offset to load a specific page.正如@jabaa 所提到的,使用offset来加载特定页面。 Look at an example below:看下面的例子:

let query = firestore.collection('col').where('foo', '>', 42);

query.limit(10).offset(20).get().then(querySnapshot => {
  querySnapshot.forEach(documentSnapshot => {
    console.log(`Found document at ${documentSnapshot.ref.path}`);
  });
});

Refer to the documentation on how to paginate a query.请参阅有关如何对查询进行分页的文档 Here you can find the examples on query pagination.在这里您可以找到有关查询分页的示例。

I managed to create some sort of pagination so that when a person clicks next button, the next set of four posts replaces the former, continuously.我设法创建了某种分页,这样当一个人点击下一个按钮时,下一组四个帖子会不断地替换前者。 However, am still figuring out on how to implement the previous button functionality so a person can load the previous sets of posts without having to refresh the page.但是,我仍在研究如何实现前一个按钮功能,以便人们可以加载前一组帖子而无需刷新页面。 I created a gist here .我在这里创建了一个要点。 You can check it out for further progress and advise.您可以查看它以获得进一步的进展和建议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过客户端JavaScript从我的node.js服务器访问数据? - How do I access data from my node.js server from client javascript? 如何将node.js Web应用程序上传到openshift? 它不是在git上,而是在我的计算机上? - How do I upload my node.js web app to openshift? It is NOT on git, but just on my computer? 如何在我的node.js Web应用程序中包含jQuery代码? - How do I include jQuery code in my node.js web app? 如何通过node.js将数据从MongoDB传递到我的web api? - How do I pass the data from MongoDB to my web api through node.js? 为什么我的本地node.js / express服务器的响应中没有得到JSON对象? - Why am I not getting a JSON object in the response from my local node.js/express server? Node.js-如何为我的Web应用设置此小服务? - Node.js - How would I set up this little service for my web app? 如何使用 node.js 和 express 将 facebook 图像保存到我的 Web 服务器文件夹中 - How to save facebook image into my web server folder using node.js and express 我正在从我的应用程序中的一个网站使用img url。 有人得到reCaptcha - I am using a img url from one website in my app. Some people gets reCaptcha 如何在 url 中使用 Node.js 添加我的网络应用程序名称? - How could i add my web App name in url use Node.js? 如何从我的 Node.JS 向我的 Javascript 客户端发送一个警报? - How can I send one alert from my Node.JS to my Javascript client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM