FAQ: Learn Node SQLite - Handling Errors Gracefully

This community-built FAQ covers the “Handling Errors Gracefully” exercise from the lesson “Learn Node SQLite”.

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

Web Development

Learn Node-SQLite

FAQs on the exercise Handling Errors Gracefully

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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!

This is probably a function scoping issue, but I’m not sure why the result is what it is.

I want to capture the ‘this.lastID’ value from db.run() in a variable (declared here as ‘lastID’) that I can then reference with a placeholder in the following db.get(). Here’s my code:

const { printQueryResults } = require(’./utils’);
const sqlite = require(‘sqlite3’);

const db = new sqlite.Database(’./db.sqlite’);

const newRow = {
location: ‘Istanbul, Turkey’,
year: 1976,
tempAvg: 13.35
}

let lastID = 0;

db.run(‘INSERT INTO TemperatureData (location, year, temp_avg) VALUES ($location, $year, $tempAvg)’, {
$location: newRow.location,
$year: newRow.year,
$tempAvg: newRow.tempAvg
}, function(error) {
// handle errors here!
if(error !== null) {
console.log(error);
return;
}
console.log(this.lastID);
lastID = this.lastID;
console.log(lastID);
});

console.log(The Last ID: ${lastID});

db.get(‘SELECT * FROM TemperatureData WHERE id = $id’, {$id: lastID}, function(err, row) {
if(err) {
console.log(err);
return;
}
printQueryResults(row);
});

The problem is that while 'lastID seems to be assigned the ‘this.lastID’ value in the db.run() callback, when it is referenced as a placeholder in db.get(), it retains its initial value of 0, and not the value assigned in db.run().

I understand that variables declared in a function have local scope only to that function, but this was an assignment, and the parser didn’t throw an error so it seems to have recognized it as an existing variable at the global level.

The only explanation I can think of if scoping isn’t the problem is that there is some sort of timing issue where my lastID variable is getting referenced before the db.run() assignment happens, but I can’t say I understand how that would be the case. Can someone help me understand why this isn’t working as expected?

You answered your own question: “sort of timing issue”. I asked myself the same question. Thank you!
To check:
const { printQueryResults } = require(’./utils’);
const sqlite = require(‘sqlite3’);
let tmp;
const db = new sqlite.Database(’./db.sqlite’);
const newRow = {
location: ‘Istanbul, Turkey’,
year: 1976,
tempAvg: 13.35
}
db.run(‘INSERT INTO TemperatureData (location, year, temp_avg) VALUES ($location, $year, $tempAvg)’, {
$location: newRow.location,
$year: newRow.year,
$tempAvg: newRow.tempAvg
}, function(error) {
// handle errors here!
if (error){
console.log(error);
return;
}
console.log(this.lastID);
tmp = this.lastID;
console.log(tmp);
});
setTimeout(() => {
console.log(“tmp:”, tmp);
db.get(“SELECT * FROM TemperatureData WHERE id=$id”,
{
$id: tmp
},
function(error, row) {
// handle errors here!
if (error){
console.log(error);
return;
}
console.log(row);
printQueryResults(row);
});
}, 2000);

I’m sorry but I don’t understand why the setTimeout() must be involved. Is this some sort of a bug in the exercise?