Get data of custom model by custom field

Builder public api key
fff8e6fa6fbc4f6abf4fb93c03e661f1

What are you trying to accomplish
I want to get data of custom model by custom field and set additional params like limit, offset etc. but can’t get whole data. Its always showing only one row in response. May be limit is not working. Can you give me an example. how can i get it in nextJS?

Code stack you are integrating Builder with
NextJS, swell SDK

Reproducible code example

builder.get('modelName', {
      limit: 5,
      offset: 5,
      query: {
        'data.productSlug': 'slug',
      }
    }).promise().then(({ data }) => {
      console.log(data);
      // Do something with the data
    }) ```

Hi @anupama.sharmaa great question!

builder.get('modelName') will only ever return 1 element, whatever the top ranked result is given your targeting options.

If you want multiple elements, then you would want to use builder.getAll('modelName', { limit: 5 } ))

Try it out and let me know if you still have issues!

Thanks for reply. It worked. but how can we get total count of model for pagination? For example: if we have 100 records of a particular slug and i want a pagination according limit of 8. In response, It doesn’t provide total count. I want to get total count with data so i can add paginate functionality.

@anupama.sharmaa Currently we do not return the full size of a query result from the database. If you wanted to set up pagination, what you would want to do is include logic based on the limit field.

One way to handle this would be, say you want to display some items on a page. You could set your API limit to be 1 over what you are looking for and if the response reachest that limit, you know there will be an extra page. If not, you know you have reached the end.

So for example if you want 10 elements on a page, set your API call limit to 11 but display only those first 10 results. If the response size is 11, you know there are more results left for another page. If the response is less than 10 you will know that you have reached the end.

Or you could set limit to 10, and if the response has 10 items, do another request and see if there are any items in the response.

If you think including the entire response size in the initial API call is a necessary feature of our API, I recommend adding it as a feature request in our user feedback platform: ideas.builder.io ! Hope that helps!

@TimG Hi Tim. I’m trying to fetch all my data from model but since the api have a limit in fetching the data, what I did was I tried to use the offset and limit options and put my codes in a loop where the offset just increments then put a checker for it to stop the loop when the api returns an empty array.

But my problem is that the api still returns data even though the offset already overlaps the full length/size of the data from a model.

For example, my model only has 40 records then I set the offset to something like 50. What I am expecting is that it will no longer return any data, but it still keeps on returning some data.
Did I set it up wrong?

Here’s my api key so you can simulate it from your end 5cde70e5453e4d12ba7e8c55a728859d

builder.getAll('blog', {
        offset: 50,
        limit: 40
    }).then(results => {
            console.log('result', results.length);
        });