This community-built FAQ covers the “Serial Queries” 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 Serial Queries
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 () 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 () below!
Agree with a comment or answer? 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!
In the solution code for this exercise, we see two patterns for error handling:
if (error) {
throw error;
}
and
if (err) {
console.log(err);
}
Is there any difference between these two expressions?
2 Likes
I feel this exercise was a little confusing.
It wasn’t clear to me that the final db.all()
call needed to be nested within the preceding db.each()
, especially as the focus of the exercise was to reduce nesting by using db.serialize()
.
I was stuck on this exercise for over an hour trying to troubleshoot it, and I couldn’t understand why my resultset from db.all()
was empty.
Some clarification as to why the final call can’t also be serialised would be great—it’s unclear to me, although I may be missing something.
12 Likes
Yes! Please, wasn’t db.all() supposed to print every row when called? Why isn’t it okay to serialize it with the other calls?
1 Like
I have the same question. I figured I could call db.all
in a serial manner but was getting an empty array. I also tried nesting it in the second db.each
callback, with the same result. I didn’t think to call it after the .forEach
in the first callback of db.each
.
1 Like
I’m also trying to understand why db.all can’t be serialized. With the database populated by the db.run, the resulting data should persist event outside of the serialize block, meaning the db.all in theory should still work even outside of the serialize block.
1 Like
WOooh , I am too figuring this out since last 1 hour.
2 Likes
No answer to this question?
I found this lesson extremely confusing and still don’t get the mechanics of the nesting/not nesting of these callbacks.
It’s like inception…
2 Likes
I hate to moan, but instructions for Node-SQL lessons are rather poor and unhelpful. They are unclear, while some hints are simply redundant, as they repeat what’s already stated in the main instructions.
Like others here I assumed the db.all() statement should be called as the last step of db.serialize(). Why must it be inside db.each() statement? I know that being the last callback it will get executed last, but wouldn’t it be clearer if it was called as just another step in db.serialize()?
Also, there seems to be inconsistency when it comes to handling errors. Some are to be thrown, some to be logged, while the last one simply ignored? Why is that?
Finally, it would be nice to see some consequence in promoting correct syntax in the official solutions. When it comes to usage of semicolons the solution for this lesson doesn’t seem to follow its own example. I know it’s not a ‘biggie’ but it can be confusing to new learners like myself wondering if my code is failing because of missing/redundant semicolon…
5 Likes
Same here perhaps tagging some experienced moderators will help?
@midlindner @vic-st ? Pleeeeeease
It would be nice if I could help, but we have no control, or real influence over the Codecademy Learning Environment. I will mention this topic to someone who may be able to look at the issues described, but I can’t make any promises that anything will change.
Thanks for pointing out these issues, and caring about the content quality.
3 Likes
Totally understand. I don’t think it’s necessarily a bug, just lack of explanation in the instructions. Why db.all is not one of the serial calls, instead it’s within db.each. I’m sure there is a reason.
2 Likes
So db.all is called int the [complete] portion of the db.each( SQL, [callback], [complete])
. So it executes after db.each is completed. The question is why is the data available only in this portion and not in a serialized sequence.
It seems that the created Average table isn’t available when it is in the serialized, and only in db.each().
Why?
I think you could do it either way. Having the last two database-methods (db.run and db.all) inside of the [complete] callback under db.each should be equal to having those two methods below and outside of the db.each-callback since db.serialize is used (but it would need to be inside of the serialized code). I think the main thing the exercise is trying to show is how .serialize is used to first create a table, and then add data (after the table is for sure created) without using a lot of nested callbacks (which would also work). I agree that this exercise was a little tricky to follow but the sqlite-node modules get easier after some more practice (in the larger projects). Also, as another option, the new paths just switched to PostgreSQL for the back-end DB lessons.
In the lesson when you have it in the same level as db.each, it doesn’t work. Should it work?
I tried and you are right - it does not work. I think the issue is that there are some lines of code such as this line that depends on the prior db commands being complete:
const averageTemperatureByYear = calculateAverages(temperaturesByYear);
Serialization does not serialize all code but it does the database queries in the right order.
db.all is a database query. It’s still confusing why having it out of the db.each complete callback
doesn’t work. I get the other variable as it’s not a query but the last query doesn’t have any direct dependencies on the variables above. It depends only on the completion of the db.each which serialize would control. Unless it doesn’t control db.each. I can’t find documentation that it only serializes db.runs to prove that.
db.run
db.run
db.each
db.run
db.all
instead it only works as:
db.run
db.run
db.each
db.run
db.all
From the sqlite3 control flow doc: “All database queries scheduled in that callback will be serialized.”
sqlite3 doc
This exercise has a helper function calculateAverages which takes the temperaturesByYear object (which is empty at first). Then this function returns an array - averageTemperatureByYear. This array is later used to insert data into the Average table. If the second call-back function (which run at the end of db.each is removed from db.each then I think db.each will still be working on getting data from the TemperatureData table and entering it in the temperaturesByYear object as the rest of the program keeps going (because only database queries are serialized - Node likes to not stop). I believe we will hit this line of code below when our temperaturesByYear object does not have the data yet. Node is pretty asynchronous in general and if we just serialized the database-queries then we will still run into a problem when getting to the line below (if it is not in the second db.each callback).
const averageTemperatureByYear = calculateAverages(temperaturesByYear);
For testing - I added a console.log(temperaturesByYear) above the quoted line. With the two call-backs (as the answer was) it logs an object with temp-data. When removing the second db.each call-back (and leaving the code that was inside of it below) this logs an empty object (like what was initially set).
3 Likes
Mike! Thanks for that explanation and testing.
I think I understand now. It seems that, if you serialize db.each and db.all, db.each will get all the data it needs from the database, and then move on to db.all BEFORE the callback functions are finalized.
So like you said, db.serialize runs the queries serially, but not the callbacks.
Does that sound right? Thanks again!
1 Like