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 7693805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:10:28+00:00 2026-05-31T21:10:28+00:00

I have created a function that will return the string path from my DB.

  • 0

I have created a function that will return the string “path” from my DB.

    function getAudio(mid, cb) {
    //mid is an array containing the id to some multimedia files.
    for(i=0; i < mid.length; i++) {

        db.transaction(function(tx) {
                tx.executeSql('SELECT * FROM Multimedia WHERE Mid=' + mid[i] + ' AND Type=' + 2, [], function(tx, results) {    
                //type=2 is audio. (type=1 is picture file etc) There is only one audiofile in the array.
                    if(results.rows.length > 0) {
                        path = results.rows.item(0).Path; 
                        cb(path);
                    }
                }, errorCB);
        }, errorCBQuery);

    }//end for
}//end getAudio()

When I remove the for-loop, the query is successfull, when the for-loops is there,
errorCBQuery or errorCB is called.

Any Ideas on how to fix this? 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-05-31T21:10:29+00:00Added an answer on May 31, 2026 at 9:10 pm

    This is the classic closure problem. transaction is an asynchronous call, which means your loop completes before the function you pass in is triggered. The function has an enduring reference to the i variable, not a copy of it as of when you called transaction. So each of those functions (you’re generating one on each loop) is seeing i == mid.length and thus mid[i] is undefined and your SQL gets messed up.

    What you need to do is have the callbacks close over another variable that won’t change when the loop proceeds. The usual way to do that is to use a factory function:

    function getAudio(mid, cb) {
        //mid is an array containing the id to some multimedia files.
        for(i=0; i < mid.length; i++) {
    
            db.transaction(makeTx(mid[i]), errorCBQuery);
    
        }//end for
    
        function makeTx(val) {
            return function(tx) {
                tx.executeSql('SELECT * FROM Multimedia WHERE Mid=' + val + ' AND Type=' + 2, [], function(tx, results) {    
                //type=2 is audio. (type=1 is picture file etc) There is only one audiofile in the array.
                    if(results.rows.length > 0) {
                        path = results.rows.item(0).Path; 
                        cb(path);
                    }
                }, errorCB);
            };
        }
    
    }//end getAudio()
    

    There, I’m passing mid[i] into the makeTx function, which returns the function that will get passed into transaction. The function we return closes over the val argument for the call to makeTx that created it, which doesn’t change.

    That’s a minimal rewrite of the code; you can probably take it further. For example, see missingno’s comment on the question re parameterized statements.


    Side note: It doesn’t look like you declare i or path anywhere. If that’s really true, you’re falling prey to The Horror of Implicit Globals. Recommend declaring them.

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

Sidebar

Related Questions

I have created a function which will convert any string into tab delimited. What's
I've created a small utility function for string conversion so that I don't have
I have created a function in PHP that calls a webservice and parses through
I have a div that is created by Ajax with .append(html) function, I'm trying
I have a function that creates zip files when passed an array of files
Lets say I have a recursive function that creates lists within lists. It return
I have created a piece of code that checks files for a user-submitted string
I have a function that reads a text file containing two lines, it reads
I have a function that creates URLs for requests as follows: public String createUrl(String
I have the following function below that will normally spit out a URL such

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.