Web SQL game - passing extra arguments to an anonymous function
-
27-10-2019 - |
Question
I'm trying to make a javascript game and I'm using WebSQL to store game data. I have created my own database selection function that formats the query results and then returns them.
In this specific instance I pass the returned object to the outcomes function. I would like to also pass in two other variables but I keep getting this error "Uncaught ReferenceError: planetInfo is not defined". If anyone could help It'd be very appreciated. Thank you in advanced.
I have also tried using "selectRowPlanets('query',outcomes(shipInfo, arrivalNumber));", but still no luck.
/** db.js **/
function selectRowPlanets(query, callBack){ // <-- extra param
var result = [];
db.transaction(function (tx) {
tx.executeSql(query, [], function(tx, rs){
for(var i=0; i<rs.rows.length; i++) {
var row = rs.rows.item(i);
result[i] = { id: row['id'],
name: row['name'],
owner: row['owner'],
colum: row['colum'],
row: row['row'],
ships: row['ships'],
production: row['production'],
percent: row['percent']
}
}
callBack(result); // <-- new bit here
}, errorHandler);
});
}
/** function.js **/
function selectDestination(shipInfo, arrivalNumber) {
selectRowPlanets('SELECT * FROM planets', outcomes(planetInfo, shipInfo, arrivalNumber));
}
function outcomes(planetInfo, shipInfo, arrivalNumber){
console.log(arguments);
}
Solution
When you run this code:
selectRowPlanets('query', outcomes(shipInfo, arrivalNumber));
The output of outcomes(shipInfo, arrivalNumber)
is passed as the callback, not the actual function.
Make an anonymous function wrapper that calls your code:
selectRowPlanets('query', function() { outcomes(shipInfo, arrivalNumber) });