Ghost JS Api is returning data in structure other than what is indicated in documenation

#1

So I am putting together a NextJS + Ghost CMS decoupled blog and am playing around with the Ghost JS API (specifically the Content API Client Library).

One of their endpoints, the api.posts.browse() one, is said to return data in this format, according to their documentation:

{
    "resource_type": [{
        ...
    }],
    "meta": {}
}

CITE: https://docs.ghost.org/api/content/#resources

In this case resource_type would be posts .

When I run my query using the following…

api.posts
    .browse({ 
      limit: 5,
      page: 1,
      include: 'authors',
      fields: 'excerpt,custom_excerpt,title,id,slug,'
    })  
    .then((posts) => {
      console.log(posts);
        return posts;
      })
    .catch((err) => {
        console.error(err);
    });

… I find that instead of a {post: [], meta: {}} format, the data is flatter.

See image from chrome JS console.

So why is this happening? Why is meta returning inside of the same array as the posts data?

#2

Hey!

The docs you linked to are for the Content API HTTP interface, this is what’s returned when making requests to the Ghost API.

The code you’ve shown is using the content-api sdk, which wraps these requests/responses, and returns Promises for arrays of content - this is designed to make it easier to work with. As for the meta this isn’t inside the array, but a property on it.

api.posts.browse(opts).then(posts => {
  posts.forEach(post => console.log(post)); // each of these is a post object
  console.log(posts.meta); // this contains meta information e.g. pagination 
});

The docs for the SDK are here: https://docs.ghost.org/api/javascript/content/

I hope this helps!

#3

Yep, your reply is very helpful.

Though I wish that the docs you linked to were more explicit about the structure of the data that those functions return. For example it would be nice to know about meta property being returned on the array.

closed #4

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