jeudi 28 mai 2015

Node-Sqlite3 - Fetching results

I am using node-sqlite3 to work with a SQLite Database from my node-js server. This works quite well, but I have an issue regarding the selection of data, which I am not able to resolve.

Here is my code:

function checkUserData(userid){
var db = new sqlite3.Database(file);    
var name = "";
db.serialize(function() {
   db.all("SELECT name FROM user WHERE user_ID = 'userid'", function(err, row) {
        if(!err){
            row.forEach( function (row) { 
                name = row.name;
                console.log("name="+name);
            });
        }
    });
});
db.close();

console.log(name);
return name;
}

The name gets fetched out of the database correctly (the "inner" console.log gives me the name), but the function does not return it. (The "outer" console.log is empty)

I want to call

checkUserData(5);

and retrieve the username to a given ID (e.g. 5)

The examples and the search results only show how to process these data in the inner function, but unfortunately they never show, how to give the results back to another function.

Can you guys help me? How do I achieve this?

Aucun commentaire:

Enregistrer un commentaire