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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:46:18+00:00 2026-05-23T21:46:18+00:00

i am using persistence.js in a project and i am having some issues with

  • 0

i am using persistence.js in a project and i am having some issues with pushing results from a query into an array.

// setup the region list array
var regionList = [];

// ping the database and grab all of the regions
var regions = Region.all().order('name');

// for each result, push the "display name" to the regions list array
regions.list(null, function(results) {
    results.forEach(function(r) {
        regionList.push(r.displayName);
    });
});

// alert the regions list array
alert(regionList);

the “alert” produces absolutely nothing, however when i try to alert within the “regions.forEach”…i get results. it seems like the scope of the array is not being maintained, but i am not certain. from my understanding, if i declare a variable outside of a function, then that variable should be available at any time until the page ends/closed…am i wrong on this? if i am running through a standard for loop, i can push dynamic data into the array – it really seems to be an issue with pushing while within a persistence.js function.

any guidance would be greatly appreciated. i could do this in my sleep with php, but this is my first go with persistence.js. thanks in advance for the assistance!

cheers!
-jon-

  • 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-23T21:46:19+00:00Added an answer on May 23, 2026 at 9:46 pm

    This has nothing to do with scoping, but rather with asynchronous callbacks.

    persistence.js is an asynchronous framework, therefore code is not necessarily executed top-to-bottom. This is a common mistake people make who just got started with asynchronous Javascript.

    With the regions.list(...) call (you can leave out the null argument, by the way), you yield execution — you basically say: whenever you retrieve the results from this query, call this function (that’s the anonymous function you pass to list). The execution then proceeds, executing the alert while regionList is still empty (because at that time, the query hasn’t finished executing yet).

    Later, when the query finishes execution the callback function is called filling the array.

    So, how to fix it: move the alert inside the callback function:

    regions.list(null, function(results) {
        results.forEach(function(r) {
            regionList.push(r.displayName);
        });
        alert(regionList);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add functionality to the v8sharp project and I'm having some issues
I'm trying to debug a workflow project that's using the default WF SQL persistence
I am attempting to pull some information from my tnsnames file using regex. I
We're running on Glassfish 3.0.1 and using Hibernate 3.5.3. Our project setup looks like
I'm working on a project where I'll be using Membase (think Memcached + persistence)
I am using EF 4.1 in a MVC 3 project and I am having
I am building a project using the Play framework and I am having trouble
I'm currently developping an application in java using Hibernate as a persistence manager and
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters

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.