I am a new CS graduate and work as an HTML5 web app developer. This form of development is new to me so pardon my incompetence on the topic... I have a javascript function that I want to return an object or change reference to the object passed, see below: NOTE: this reads all the values stored in the passed "table" within my indexed DB

My original thought was to create the object in the above function and then return it. The problem here is that the elements are added to the object in the scope of the onsuccess function... so then I thought I could pass the object by reference to solve the issue which I like but I seem to be missing some key javascript insight because I cant figure out why the original object isn't being changed. I am interested in any solution, whether solving my pass by reference dilemma or showing me the right way to refactor this so that it can return the object.

Thanks,

11-28-2013, 03:17 AM

bionoid

I am going to try help, but making a couple assumptions on how your function is being used right now.
From what it looks like, you are calling the readAll function, passing in a blank array by reference, then trying to access the members of that array immediately afterwards:

If I'm guessing right the db object uses asynchronous requests back to the webserver, and if this is the case then you will need to wait until your readAll function is completely done fetching the records by using a callback function instead:

I couldn't test any of this code, but I tried hard not to make mistakes.

11-29-2013, 12:36 PM

vinnyzwrx

Thanks, that's exactly what I was looking for however I still am having trouble figuring out how to return the object in the callback function. in the end I need the result of the obj that gets populated in readAll().

11-29-2013, 12:38 PM

vinnyzwrx

I guess I should have said that I am printing the object to the screen in the end... Seems that the callback function doesn't like the document.write() function...

11-29-2013, 03:28 PM

vinnyzwrx

After a little bit of reading and learning about jQuery I have figured out a way to do this...