11-22-2022-Figuring-Out-The-Weird-Return

Figuring Out The Weird Return

Project

Work Project

Date

11/22/2022

Technology

  • JavaScript

The Problem

Got stuck troubleshooting a piece of code that was returning successfully even with bad values.

The Process

  • First I console.logged everything to confirm what was coming up.
  • I saw that I was returning the data correctly from the database
  • I saw that the if statement was being called by putting a debug message in. However, I was still getting success messages for bad values
  • I paired with my supervisor to see what is up and he pointed out that the code block below was returning that code block, not the scope of the function.

Solution

for (const entry of entries) {
    const found = notes.find((notesEntry) => entry.Id === noteEntry.Id);
    if (!found) {
      console.debug('NOT FOUND!')
      return ...
    }
    console.debug('FOUND!');
  }

The forEach loop I was using created its own scope which was what was being returned each time. I did not realize that until someone pointed it out. This could be avoided by doing a for...of loop rather than a foreach loop.

#ProblemSolving