shadsauhduisad
shadsauhduisad

Reputation: 141

transaction.executeSql Success function error

I did a function that select the data from db and show an alert, but I'm getting the following error in the Success function:

Cannot read property 'rows' of undefined retrieveData

Here is my functions

 function selectAll(){
    DB.transaction(
function(transaction) {
    transaction.executeSql("SELECT * FROM CDprodutos", [], retrieveData()); //The retrieveData function is where the data is sent/returned to in parameters (transation, results)
});
}
function retrieveData(transaction, results) {
   for(var i = 0; i < results.rows.length; i++) {

        var data = results.rows.item(i)['id']
        alert(data.value);
    }
}

Upvotes: 0

Views: 3028

Answers (1)

tymeJV
tymeJV

Reputation: 104775

It looks like your function is being executed immediately and also there is no data being passed to it, so obviously the rows property is coming from undefined. By writing () after retrieveData, you are invoking the function immediately. Remove the () to use this as a callback.

It also appears you need to pass data along to it, so an anonymous function would probably do the trick here:

function selectAll(){
    DB.transaction(
        function(transaction) {
            transaction.executeSql("SELECT * FROM CDprodutos", [], function() {
                retrieveData(transation, results); //not sure where results comes from? parameter of function??
        });
    });
}

function retrieveData(transaction, results) {
    for(var i = 0; i < results.rows.length; i++) {
        var data = results.rows.item(i)['id']
        alert(data.value);
    }
}

Upvotes: 1

Related Questions