0

In the following code, how do I make the variable songlist available to the callback function complete?

If I run the code as is, sqlite successfully populates the songlist, but when the callback is run the variable becomes null.

Basically, I'm trying to figure out a way to ensure sqlite completes running before console.log(songlist) runs. I thought using a callback would do it, but then run into this variable passing issue. Thanks.

    var sqlite3 = require('sqlite3').verbose();
    var db = new sqlite3.Database('mydb.db');

    var songlist = []
    var complete = function (songlist) {
        console.log(songlist);
    }
    db.serialize(function () {
        db.each("SELECT rowid AS id, info FROM lorem where info like '%" + song + "%'", function (err, row) {
            songlist.push(row.info);
            console.log("each")
        }, complete)

    })
0

2 Answers 2

1

In your current code songlist is global, which means it's already available to your callback. When you add the songlist argument to the callback you are shadowing it which makes the global no longer visible.

You could simple say:

var complete = function () {
    console.log(songlist);
}

and log the global object.

But that's not very satisfying because you should generally avoid globals when not absolutely required.

A better idea is to define songlist in the function and pass it to your callback when you're done:

var complete = function (songlist) {
    console.log(songlist);
}
db.serialize(function () {
    var songlist = []

    db.each("SELECT rowid AS id, info FROM lorem where info like '%" + song + "%'", function (err, row) {
        songlist.push(row.info);
        console.log("each")
    }, function(){
          complete(songlist)
       }
    })
)}

Of course you don't really need a separate function — you could just log the songfest directly in the callback to your db function.

Sign up to request clarification or add additional context in comments.

Comments

0

By creating an anonymous function that passes the songlist to complete as shown bellow:

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('mydb.db');

var songlist = []
var complete = function (songlist) {
    console.log(songlist);
}
db.serialize(function () {
    db.each("SELECT rowid AS id, info FROM lorem where info like '%" + song + "%'", function (err, row) {
        songlist.push(row.info);
        console.log("each")
    }, function() { complete(songlist) })

})

2 Comments

Thanks, your solution works too, although using the global variable seems simpler.
Absolutely, being in the global scope, there is no need and the first solution is simpler. The anonymous function technique however will work in all situations where you don't use global variables. That might be the case for you in the future when your application grows and everything is modularized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.