Pagination function not working even though there are more than 2 items in dataset

const linkField = "link-ourcourses-1-title"; // replace this value

$w.onReady(function () {
  $w("#dynamicDataset").onReady(() => {
    const numberOfItems = $w("#dynamicDataset").getTotalCount();

    $w("#dynamicDataset")
      .getItems(0, numberOfItems)
      .then((result) => {
        const dynamicPageURLs = result.items.map((item) => item[linkField]);
        local.setItem("dynamicPageURLs", dynamicPageURLs);
      })
      .catch((err) => {
        console.log(err.code, err.message);
      });
  });
});
$w.onReady(function () {
  $w("#preBtn").disable();
  $w("#nextBtn").disable();

  if (local.getItem("dynamicPageURLs")) {
    const dynamicPageURLs = local.getItem("dynamicPageURLs").split(",");

    const currentPage =
      "/" +
      wixLocationFrontend.prefix +
      "/" +
      wixLocationFrontend.path.join("/");
    const currentPageIndex = dynamicPageURLs.indexOf(currentPage);

    if (currentPageIndex > 0) {
      $w("#preBtn").link = dynamicPageURLs[currentPageIndex - 1];
      $w("#preBtn").enable();
    }

    if (currentPageIndex < dynamicPageURLs.length - 1) {
      $w("#nextBtn").link = dynamicPageURLs[currentPageIndex + 1];
      $w("#nextBtn").enable();
    }
  }
});

it can change to next item sometimes, but the previous item button is not working. Anyone knows how to fix it?

Question: Why do you use 2x –> $w.onReady() ??? –> You have only one page.

Ask yourself –> WHAT DOES MEAN –> $w.onReady() ← exactly?

Could it be, that your SETUP has timing problems because of the 2x $w.onReady() ???

Try ….

import wixLocationFrontend from 'wix-location-frontend';
import { local } from 'wix-storage';

$w.onReady(function () {
  const linkField = "link-ourcourses-1-title"; // replace this value
  const dataset = $w("#dynamicDataset");

  dataset.onReady(() => {
    const totalCount = dataset.getTotalCount();

    dataset.getItems(0, totalCount)
      .then((result) => {
        const dynamicPageURLs = result.items.map((item) => item[linkField]);
        local.setItem("dynamicPageURLs", dynamicPageURLs.join(","));

        const currentPage = "/" + wixLocationFrontend.prefix + "/" + wixLocationFrontend.path.join("/");
        const currentIndex = dynamicPageURLs.indexOf(currentPage);

        // Disable both by default
        $w("#preBtn").disable();
        $w("#nextBtn").disable();

        // Enable previous
        if (currentIndex > 0) {
          $w("#preBtn").link = dynamicPageURLs[currentIndex - 1];
          $w("#preBtn").enable();
        }

        // Enable next
        if (currentIndex < dynamicPageURLs.length - 1) {
          $w("#nextBtn").link = dynamicPageURLs[currentIndex + 1];
          $w("#nextBtn").enable();
        }
      })
      .catch((err) => console.log("Error:", err));
  });
});