Array won't filled out

i have a curious problem.

I've fetch rows from the database and insert it to an Array. But Array.push() won't work.

Source

var donations_data = [];

/* Get Donations */
instance.server.db.query("SELECT * FROM `donations` WHERE `userid`='" + instance.user_data.userid + "' AND `visible`='1' ORDER BY `date` DESC LIMIT 10", function(error, rows, fields) {
    if(rows.length > 0) {
        _.each(rows, function (entry, index) {
            console.log(">>>>>>>>>>>>>>>>>>>>>");
            console.log(entry);
            console.log(">>>>>>>>>>>>>>>>>>>>>");

            donations_data.push({
                username:           entry.from_username,
                note:               entry.note,
                timestamp:          entry.date,
                amount:             entry.amount,
                currency:           entry.currency,
                currency_symbol:    '?',
                transaction_id:     entry.transaction_id,
                paypal_email:       entry.from_paypal_email
            });
        });
    }
});

console.log("==================================");
console.log(donations_data);
console.log("==================================");

Output

>>>>>>>>>>>>>>>>>>>>>
{ id: 4,
  userid: 1,
  from_username: 'user 2',
  from_paypal_email: 'demo@example.com',
  transaction_id: '12345',
  status: 'COMPLETED',
  note: 'No Message',
  date: 1386012587,
  amount: '5.00',
  cent_amount: 0,
  an_dt: '',
  currency: 'EUR',
  visible: 1,
  date2: Mon Dec 02 2013 20:29:47 GMT+0100 (CET),
  new: 0 }
>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>
{ id: 3,
  userid: 1,
  from_username: 'USer 1',
  from_paypal_email: 'demo@example.com',
  transaction_id: '23ewqs',
  status: 'COMPLETED',
  note: 'Das ist ein Test',
  date: 1386012427,
  amount: '5.00',
  cent_amount: 0,
  an_dt: '',
  currency: 'EUR',
  visible: 1,
  date2: Mon Dec 02 2013 20:27:07 GMT+0100 (CET),
  new: 0 }
>>>>>>>>>>>>>>>>>>>>>
==================================
[]
==================================

Can you tell me why the array won't be filled? The data will printed out, therefore must work...

You're trying to log the results of adding the objects to the array before the asynchronous instance.server.db.query method has had a chance to complete. Use a callback to get at the data once the loop has completed

var donations_data = [];

function doQuery(callback) {
  instance.server.db.query(SQL, function(error, rows, fields) {
    // add data to array with loop
    // call callback
    callback(donations_data);
  });
}

doQuery(function (donations_data) {
  console.log(donations_data);
});