0

I programmed in a few programming languages, from time to time i have some experience also with Javescript.

Here my situation: I have an array as global variable (myArray). I need to refill myArray with data from the DB when some event happens. So I wrote myFunction, in which myArray is emptied to remove eventual older data, then filled again with the new data.

If I don't delete the array content, everything works, but (of course) the array may contain also older data and will grow each time the function is called, and I don't want that.

What I don't understand is the following: If I delete the content of myArray when myFunction starts, I cannot refill it again...

Here the simplified version of my code, thanks everyone for any help:

myArray = []; // global variable

// IN MYFUNCTION
//first empty the array, then refill it with data from DB
{
//  Let's empty myArray, tried all 4 different ways to do that -- no difference:

// Way 1)
//while(myArray.length > 0) {  myArray.pop(); }

//OR Way 2)
//myArray.length=0;

//OR Way 3)
//myArray.splice(0,myArray.length);

//OR Way 4)
myArray = [];

    dataNamesQuery = "SELECT something FROM db according user's input";

    DatabaseInterface.get_data_now(dataNamesQuery).get({}, function(result){
        allDataNames = result.Results;

        for(i in allDataNames){
            myArray.push(allDataNames[i].something);
        }
    });
 }
//IF I DELETE THE CONTENT OF MYARRAY, THIS WILL PRINT NOTHING;
//ELSE IF I DON'T DELETE ITS CONTENT, THE ARRAY CONTAINS NEW AND OLD DATA
    for(ix in myArray){
        console.log("in myArray got " + myArray[ix]);
    }

} //MYFUNCTION END

--edits--

  1. I see that this question is marked as duplicated, maybe it is, just I couldn't find a question that suited to this situation.

  2. I see from answers you listed that maybe the problem is in synchronization, so I have to learn about this topic

fresko
  • 1,890
  • 2
  • 24
  • 25
  • 2
    Try moving the for loop which logs myArray inside of the result function of the database to see if it will log there – Simon Sep 21 '15 at 08:05
  • Are you running `dataNamesQuery` directly in the DB?!?!? – Jonathan Sep 21 '15 at 08:07
  • @Simon Yes, moving the loop in the result function allows me to see that myArray is full. But why is it empty after that function, since it is a global variable? – fresko Sep 21 '15 at 08:36
  • @Jonathan the query is run in DB by another function (get_data_now) – fresko Sep 21 '15 at 08:37
  • Ok, and are you aware of [SQL injection](https://www.owasp.org/index.php/SQL_Injection)? – Jonathan Sep 21 '15 at 11:01
  • @fresko It is empty 'after' the function because it is actually 'before' the function, most of the time. The array is filled when the database returns values but you don't know when that will happen. Your program will keep on running and will most likely hit the logging before the array is filled. If you log inside the function where you fill the array, you know you have the data. This is explained more thoroughly in the duplicate questions – Simon Sep 22 '15 at 10:24
  • Yes i am learning now about asyncronous calls. But i solved in different way. I changed the query to DB to have all possible data and fill the array only once with more rows (changed as multidimensional array). So instead to make many, more precise queries when the input changes (that required to empty and refill the array), i do only one at the begin and then select what i need on the ready array, according to the input. This solution is fine for my case, of course it is only a workaround. – fresko Sep 23 '15 at 10:14

0 Answers0