FAQ: Code Challenges - Code Challenge 4

This community-built FAQ covers the “Code Challenge 4” exercise from the lesson “Code Challenges”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn Node-SQLite

FAQs on the exercise Code Challenge 4

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

The challenge question #4 is…
The query in the workspace is going to return an error! Log the error to the console if it exists, otherwise log the retrieved rows.

Can someone solve this riddle for me. Why isn’t my answer acceptable? See both my answer and the correct answer below. Note I pulled the syntax for my answer from the exercise on error-handling(https://www.codecademy.com/courses/learn-node-sqlite/lessons/learn-node-sqlite/exercises/handling-errors).

/* my first attempt was rejected. */
db.all(‘SELECT * from NonexistentTable’, (err, rows) => {
if (err) {
return console.log(err);
}
console.log(rows);
});

/* the corrected solution. */
db.all(‘SELECT * from NonexistentTable’, (err, rows) => {
if (err) {
console.log(err);
} else {
console.log(rows);
}
});

I’m VERY disappointed on this.
Is there an explanation as to why the following is not accepted??

const db = require('./db');

db.all('SELECT * from NonexistentTable', (err, rows) => {
  if (err) {
    console.log(err);
    return;
  }
  console.log(rows);
});
1 Like

Hey @mariaburmeister , @peacefulrick ,
I went here for the same reasons as you. In the linked exercise it clearly states:

Log an error if one is passed into the callback and return to break out of the callback;

With the following code:

if (error) {
  return console.log(error);
}

And truth is: that would actually run. Not sure why Codecademy’s module doesn’t accept it, but it sure is confusing/frustrating for us at times. I am in my last revisions of the Web Dev course and I’ve got to say I encountered quite of few of those situations along the way.

Would be nice if Codecademy reads this and makes their answer checkpoints more flexible!

1 Like

Had the exact same problem. And the only error message is just “Did you log the error if it exists and the rows if there is no error?”

Yes. Yes, I have.

Turns out that in this case, they want the error handing to use if/else instead of an if statement with a return.