I am fairly certain this is a ten penny closure question. But having read a few articles on closures, I still cannot get this to work.
character is always returned as ‘Z’. character is also a global variable.
I need “render” to remember the character in the loop:
populateList: function()
{
var render = function(tx, result)
{
console.log(character);
for (var i = 0; i < result.rows.length; i++)
{
var contact = result.rows.item(i);
console.log(contact.Name);
}
}
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(var i = 0; i < str.length; i++)
{
var nextChar = str.charAt(i);
database.open();
var sql = "SELECT Name FROM Contact WHERE Name LIKE \'" + nextChar + "%\' ORDER BY Name";
database.query(sql, render);
}
}
Usage:
Edit:
Also, asawyer is correct in his comment above — assuming you are using node-mysql, it has support for parameterized queries:
Use that and save yourself some trouble!