Trying to get 'meta object from posts.browse()

Hi,

I’m recently working on local development with ghost + nextjs. and I’m totally new on the API things so I’m trying my best to understand it.

Goal:

Want to create a pagination for my archive page

Problem:

Can’t get meta object from posts.browse()

/* ghost.js */

export async function getPosts(limit, page) {
  return await api.posts.browse({ limit: limit ? limit : 'all', page: page ? page : 1 }).catch((err) => {
    console.error(err)
  })
}

Result:

undefined return in console.log

/* latest.js */

export async function getStaticProps() {
  // How many posts should appear on the page
  const postsPerPage = 6

  // Declaring a new variable (posts/pages) and set the data into the variable.
  const posts = await getPosts(postsPerPage)

  if (!posts) {
    return {
      notFound: true,
    }
  }

  return {
    props: { posts }
  }
}
/* latest.js */
export default function Latest({ posts }) {
  useEffect(() => {
    console.log(posts.meta)
  });

return (
    <Main>...

Reading from official docs doesn’t not solve my problem.

Postman:

But in Postman I can see there’s a meta property.

What happens if you log the result at the initial getPosts function or within the getStaticProps()? Do you see the whole posts array as you’d expect?

Hi Ryan,

Yes, if I log the getPosts I’ll get meta property.

Code:

latest.js

export default function Latest({ posts }) {

  useEffect(() => {
    const postsPerPage = 6
    console.log(getPosts(postsPerPage))
  });

  return (
    <Main>...

Log results

Progress. That tells you that the Ghost side of things is working and that bug is in how you’re passing data around your Next js app.

1 Like