I'm using node, javascript, vscode, sqlite3. I'm trying to grab the rowid after I insert a game record so I can use that as a foreign key to insert the details of the game. The record inserts, and this.lastID contains the rowid but no matter how I try my copy of rowid erases itself as soon as this.lastID goes out of scope.
//databaseinterface.js
function InsertNewGame() {
let returnvalue;
db.run("INSERT INTO Games DEFAULT VALUES", function(err) {
if (err) {
console.log(err.message);
}
// get the last insert id
else {
returnvalue = this.lastID;
console.log ("return value set at " + returnvalue);
}
});
console.log("but at the end of insertnewgame return value is: " + returnvalue);
return returnvalue;
};
module.exports.InsertNewGame = InsertNewGame;
//postheader.js
var express = require('express');
var bodyParser = require('body-parser');
const Database = require('../public/javascripts/databaseinterface');
var router = express.Router();
router.use(bodyParser.json());
router.post('/', function(req, res, next){
let gameid = Database.InsertNewGame();
console.log("over in the router there is no sign of the returnvalue: " + gameid)
res.send("Game " + gameid + " inserted");
});
module.exports = router;
In the console I get:
but at the end of insertnewgame return value is: undefined
over in the router there is no sign of the returnvalue: undefined
return value set at 16
why is returnvalue resetting? does it have anything to do with the console logs not being done in order?