1

I have a phonegap db functioning JS script below. I want to return the final string variable "feeds" outside the whole function. This only returns "undefined". Please help me with required changes to return the "feeds" variable.`

function getProviders() {
    var feeds = "";
    var db = window.openDatabase("db", "1.0", "desc", 1000000);

    db.transaction(function(tx) {
        var db = window.openDatabase("db", "1.0", "desc", 1000000);
        tx.executeSql("SELECT * FROM `feed_provider`", [], function(tx, results) {
            var len = results.rows.length;

            for (var i = 0; i < len; i++) {
                feeds += results.rows.item(i).id + "|" + results.rows.item(i).name + "|" + results.rows.item(i).status + "|" + results.rows.item(i).feed_url + ",";
            }
        }, sqlerror);
    }, sqlerror2);
    return feeds;
}
2
  • @techytree: Is there any reason why you are not using callbacks? Commented Oct 3, 2013 at 6:47
  • @karthick: I dont know much how to use callbacks. Commented Oct 3, 2013 at 13:15

1 Answer 1

5

I'm going to assume either db.transaction or tx.executeSql is async, in which case I would use a deferred:

function getProviders() {
    var feeds = "";
    var def = $.Deferred();
    var db = window.openDatabase("db", "1.0", "desc", 1000000);

    db.transaction(function(tx) {
        var db = window.openDatabase("db", "1.0", "desc", 1000000);
        tx.executeSql("SELECT * FROM `feed_provider`", [], function(tx, results) {
            var len = results.rows.length;

            for (var i = 0; i < len; i++) {
                feeds += results.rows.item(i).id + "|" + results.rows.item(i).name + "|" + results.rows.item(i).status + "|" + results.rows.item(i).feed_url + ",";
            }
            def.resolve(feeds);
        }, sqlerror);
    }, sqlerror2);
    return def.promise();
}

Call it like this:

getProviders().done(function(feeds) { 
    // do something with feeds
});
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried this and now it returns an object which shows [object Object] when alerted. How can I use the string in 'feeds' variable through this object? Think that I want to alert the string value in feeds variable. How can I do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.