0

I'm using sqlite3 in node.js and want to make functions which return specific data.

Here is an example where a function returns the last inserted row id.

The id is correctly retrieved, and I can output it in console.log, but how do I get its value back into my function so I can return it?

All the examples I find merely output the value here with console.log.

  _getLastInsertRowId = function () {
    var id = 0;
    var that = this;
    db.get("SELECT last_insert_rowid() as id", function (err, row) {
        id = row['id'];
        //that.id = row['id']; //DOESN'T WORK
        console.log(id); // shows correct id
        //return row['id']; //DOESN'T WORK
    });
    return id; // DOESN'T WORK, ID IS ALWAYS 0
  }
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • You cant return the value directly, you either use a callback, or better still use promises. – Keith Jun 07 '18 at 14:58
  • You can't. Just the same as you can't read tomorrows newspaper. (And I'm surprised that no one downvoted this just as some people do with similar questions by low-rep users. As this question shows asynchronous javascript is a very complicated concept that is difficult to grasp even for advanced coders) – Jonas Wilms Jun 07 '18 at 14:58

0 Answers0