Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

335
Views
Unhandled Promise Rejection: TypeError: undefined is not an object

I am attempting to go into a restaurant database and get 15 random restaurants printed to the console. Here is the code I am using:

function getRandomIntInclusive(min, max) {
  const newMin = Math.ceil(min);
  const newMax = Math.floor(max);
  return Math.floor(Math.random() * (newMax - newMin) + newMin);
}

function dataHandler(dataArray) {
  console.log("fired dataHandler");
  console.table(dataArray);
  const range = [...Array(15).keys()];
  const listItems = range.map((item, index) => {
    const restNum = getRandomIntInclusive(0, dataArray - 1);
    return dataArray[restNum];
  });
  
  console.log(listItems);
  
  //range.forEach((item) => {
    //console.log("range item", item);
    //return[dataArray]
  //});
}

async function mainEvent() {
  console.log("script loaded");
  const form = document.querySelector(".main_form");
  const submit = document.querySelector(".submit_button");

  const results = await fetch(
    "https://data.princegeorgescountymd.gov/resource/umjn-t2iz.json"
  );
  const arrayFromJson = await results.json();
  console.log(arrayFromJson);

  if (arrayFromJson > 0) {
    submit.style.display = "block";
    form.addEventListener("submit", async (submitEvent) => {
      submitEvent.preventDefault();
      console.log("form submission");
      dataHandler(arrayFromJson);
    });
  }
}

The problems lies with restNum and returns an error that says:

Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating 'dataArray[restNum]')

I was wondering if anyone is able to help me debug this. Thank you in advance!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!