Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7807497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:52:13+00:00 2026-06-02T02:52:13+00:00

I have this function that seems to be doing what it should: var getData

  • 0

I have this function that seems to be doing what it should:

var getData = function(query)
{
    var data = [];
    db.transaction(function(tx) {
        tx.executeSql(query, [], function (tx, results) {
            var len = results.rows.length, i;
            for (i = 0; i < len; i++) {
                data.push(results.rows.item(i));
            }
        }, error);
    });

    return data;
}

When I do a console.log(data), it shows an array of objects with correct data, as you can see in an example from Chrome’s console:

Chrome's console showing that the data is correct

However, data.length is 0, making it impossible to iterate. I’ve tried

for(var i = 0; i < data.length; i++) {
    $('.regions').append(data[i].region + '<br>');
};

which is no good since data.length is 0. I’ve tried

for(var key in data) {
    $('.regions').append(data[key].region + '<br>');
};

which doesn’t work, presumably for the same reason. I even tried $.each() from jQuery, but nothing even enters the loop. I do a console.log(data) right before these loops to double check it still has the data as I would expect. I manually recreated the data by hand with

data = [
    {
        available: "1",
        company_id: 1,
        cultivar: "Cultivar 2.0",
        cultivar_id: 18,
        id: 18,
        image: "b0c2dd4765422fc0acce9461a040ebaf.png",
        logo: "980f2a610d681ade5b5b42511f89655c.png",
        maintenance: "Fairway",
        maintenance_id: 7,
        name: "Cultivar 2.0",
        region: "Midwest",
        region_id: 24,
        species: "Perennial Ryegrass",
        species_id: 7,
        trait_disease: "Disease 2",
        trait_disease_id: 6,
        available: "1"
    },
    {
        company_id: 1,
        cultivar: "Cultivar 2.0",
        cultivar_id: 18,
        id: 18,
        image: "b0c2dd4765422fc0acce9461a040ebaf.png",
        logo: "980f2a610d681ade5b5b42511f89655c.png",
        maintenance: "Fairway",
        maintenance_id: 7,
        name: "Cultivar 2.0",
        region: "Midwest",
        region_id: 24,
        species: "Perennial Ryegrass",
        species_id: 7,
        trait_disease: "Disease 2",
        trait_disease_id: 6
    }
];

and that type of thing appeared exactly the same in the console, but it worked when I iterated over it. I have done a few other things I found around StackOverflow, including making sure that it indeed was recognized as an array.

Even if I knew the length, data[0] is undefined. I have spent some sweet time on this and wondered if anything else had any ideas.

This has been tested in Chrome 18.0.1025.151 (Firefox doesn’t have sqlite) and it will eventually go onto a phone with jQuery Mobile and PhoneGap, but we’re not at that point, yet. Just a simple HTML page with a sqlite database.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:52:15+00:00Added an answer on June 2, 2026 at 2:52 am

    Your fundamental problem is that you’re expecting to be able to return the result of an asynchronous operation from the function that initiates it.

    The call to “.executeSql()” returns immediately. At some point after that, the database operation completes and your callback function is invoked. However, the containing function (“getData()”) has long since returned.

    The way to make an API around asynchronous operations is to make your own API asynchronous. Give “getData()” another parameter so that its clients can pass in a handler function. Then, from the callback, invoke the handler and pass it the array as a parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function in jQuery that gets data from a page with POST,
I have this function that prints the name of all the files in a
I have this function that converts all special chars to uppercase: function uc_latin1($str) {
I have this function that takes the input of a, runs a calculation and
I have this function that switches the HTML contents from one element on a
So I have this function that forks N number of child processes. However it
I have this function called ProperCase that takes a string, then converts the first
I have this function on my controller (Im using CodeIgniter) that reads the database,
I have this function to edit all fields that come from the form and
I have this function in a Code Igniter model that creates a new video.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.