1

I use sqlite3 module for node.js and I get values from table with cursor, like this:

db = new sqlite3.Database(config.sqlite3config.path);

statement = db.prepare("SELECT * FROM test_table");

var temp = {};
statement.get(function(err, col) {
   temp = col;
});

console.log(temp);

At last console.log I get empty js-object, but I want to get statement.get function result, how I can get col value from callback?

user3012872
  • 25
  • 1
  • 4

4 Answers4

2

you only can use the value after the callback is called...

db = new sqlite3.Database(config.sqlite3config.path);

statement = db.prepare("SELECT * FROM test_table");

var temp = {};
statement.get(function(err, col) {
   temp = col;
   console.log(tempo); // here works!...
   //do your code here!
});

console.log(temp); //here isn't work
Luan Castro
  • 1,184
  • 7
  • 14
  • Thanks for answer, but I need to get value after statement.get function at another part of the code... – user3012872 Nov 21 '13 at 20:24
  • yo can't do this because the value not exist before the callback returns... the solution is you create the logic based in callback's – Luan Castro Nov 21 '13 at 20:26
2

The reason being because that DB call is asynchronous -- so when your console.log hits, that command hasn't completed yet. You have a couple of options, 1). Do your work in the callback function of that command:

statement.get(function(err, col) {
     temp = col;
     //do stuff!
});

2) Use a callback function and pass that data into it:

statement.get(function(err, col) {
     temp = col;
     callback(temp);
});

function callback(param) {
    console.log(param);
}
tymeJV
  • 103,943
  • 14
  • 161
  • 157
0

Your problem is that you're running a non-blocking call. So temp is still going to be an empty object until the db call returns.

Here's what you want to do:

var db = new sqlite3.Database(config.sqlite3config.path),
    temp;

statement = db.prepare("SELECT * FROM test_table");

statement.get(function(err, col) {
    temp = col;
    console.log(temp);
}); 

There's no reason to set temp to an empty object, because col will be overriding it anyway, so just declare it as a variable and you'll be good. What I would do would be to not even set temp, and just use col.

but if you absolutely need to do it the way you did you might want to do it a hack-ey way and set a timeout:

db = new sqlite3.Database(config.sqlite3config.path);

statement = db.prepare("SELECT * FROM test_table");

var temp = {};
statement.get(function(err, col) {
   temp = col;
});

setTimeout(function () {
    console.log(temp);
}, 20);

The problem with doing it this way is that you have no idea when the return will get back if at all.

Brian Noah
  • 2,962
  • 18
  • 27
0

Reading your comments, you might want to modify your code this way :

db = new sqlite3.Database(config.sqlite3config.path);

statement = db.prepare("SELECT * FROM test_table");

statement.get(doSomethingElse);

function doSomethingElse(err, temp) {
  // your code here
  console.log(temp);
}
Jerome WAGNER
  • 21,986
  • 8
  • 62
  • 77