Next Post — Node

Hello guys, I'm trying to create a "next button" for the posts on my site but when I reach the last post I get an error, anyhow I can get the first post when there's no more posts left?

app.get('/detail/:uid', async (req, res) => {
  const api = await initApi(req)
  const work = await api.getByUID('work', req.params.uid)
  const nextwork = await api.query(Prismic.Predicates.at('document.type', 'work'), { pageSize : 1 , after : `${work.id}`, orderings: '[my.work.date desc]'})

  res.render('pages/detail', {
    work,
    nextwork
  })
})

Thanks!

Hello @Kero

Welcome to the Prismic community, and thanks for reaching out to us.

If I understand your question well, you want to use the first post in case there is no post left. In this case, you will have to send a new query to retrieve the first result when the last post is retrieved from the query.

Steps:

  1. Check if the result_size is zero or not.
  2. If it is zero, then send a new request for retrieving the first element. (by removing the after parameter from the query and setting page size as 1)
  3. If it is not zero, then retrieve the result.

Let me know if you have any further questions.

Thanks,
Priyanka

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.