How to get the full topic list from the Category API

Solved Technical Support
  • Hi,

    I'm using api/topic/{category_id} API and it is only returning the first 40 topics. I've tried to figure out how the lazy loading works but can't see any network request in Chrome developer tools.

    I'd love some help with how to get the full list or how to get the next 40. My largest category has 248 topics so I think it would be fine to get the full list.

  • Hi Joel,

    You can write some javascript code to get all the topics in a category, here is a sample that loops until all pages are loaded.

    const topics = [];
    let done = false;
    let page = 1;
    while(!done) {
      const d = await $.get(`/api/category/21/tutorials?page=${page}`);
      topics.push(...d.topics);
      if (d.pagination.next && d.pagination.next.page > page) {
          page = d.pagination.next.page;
      } else {
          done = true;
      }
    }
    console.log(`done, got ${topics.length} topics`);
    
  • Thank you! that is exactly what I needed.

  • Joel HaggarJ Joel Haggar has marked this topic as solved on

Suggested Topics


  • 0 Votes
    9 Posts
    539 Views

    @8shlomi

    What version are we talkin' about here?

    It is an unexpected behavior unless there are no categories created or there is a bug in play.

    Do also check yout categories setup in the ACP and also the permissions/privileges too

    Naturally if you are admin you should by rights see everything.

  • 0 Votes
    2 Posts
    597 Views

    Anyone please give little bit of clue. How a topic works in nodebb?

  • 0 Votes
    9 Posts
    2k Views

    Have you tried run ./nodebb setup or ./nodebb build? It seems that it did't build the static files.

  • 2 Votes
    5 Posts
    2k Views

    @MJ My solution. For demo you can look at my forum (russian language)

    WARNING!!! It is dirty solution (change source code may cause problem with updating in future, you must know how to solve it) and I am not recommend use it, if you didn't understand what you do. Solution without localization, use it only if your forum use only one language (or change code and use number month)

    1. Set cutoff settings for timeago library

    Open acp > custom HTML&CSS > Custom Header and add this:

    <script type="text/javascript"> jQuery(document).ready(function() { jQuery.timeago.settings.cutoff = 2419200000; }); </script>

    In this example I set 28 days for relative date (1000 * 60 * 60 * 24 * 28 = 2419200000), you can change it.

    Didn't forget turn on checkbutton "Enable Custom Header" and save changes. After this, all dates older 28 days become unvisible.

    2 Change timeago library

    Onen file public/vendor/jquery/timeago and this code:

    if (!isNaN(data.datetime)) { if ( $s.cutoff == 0 || Math.abs(distance(data.datetime)) < $s.cutoff) { $(this).text(inWords(data.datetime)); } }

    replace to this:

    if (!isNaN(data.datetime)) { if ( $s.cutoff === 0 || Math.abs(distance(data.datetime)) < $s.cutoff) { $(this).text(inWords(data.datetime)); } else { var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" ]; var day = data.datetime.getDate() var year = data.datetime.getFullYear(); var monthIndex = data.datetime.getMonth(); var hours = ("0" + data.datetime.getHours()).slice(-2); var minutes = ("0" + data.datetime.getMinutes()).slice(-2); $(this).text(day + ' ' + monthNames[monthIndex] + ' ' + year + ' ' + hours + ':' + minutes); } } 3. Restart forum

    All must work fine

    Revert changes back

    If you need revert changes, you can do it with command

    git checkout /public/vendor/jquery/timeago/jquery.timeago.js

    And remove added text from acp > custom HTML&CSS > Custom Header

  • 0 Votes
    4 Posts
    3k Views

    Also /src/views/, for ACP templates