Tecnico Tecnico - 24 days ago 10
Node.js Question

How to do callback having some lines in my query

I have a function like this:

function get_users (v_user, callback){
stmt = db.prepare("SELECT name FROM table_users where option = ?");
stmt.bind(v_user);

stmt.get(function(error,row){
if(row){
console.log(row.name) // only first line
}


My problem is that, My query affect three lines (three names) and I need to access to all my results. Somebody know how to solve it?
Thanks in advance.

Best regards.

Answer

according to the documentation you must use stmt.all() to get all results.

function get_users (v_user, callback){
  stmt = db.prepare("SELECT name FROM table_users where option = ?");
  stmt.bind(v_user);

  // instead of stmt.get()
  stmt.all(function(err,rows) {
    if (err)
      return callback(err);
    return callback(null, rows);
}