I read this and tried implementing my function so that data doesn't change back, but it isn't working with me.
I have an array of objects, where I send them one by one to another function, to add data.
queries.first(finalObject.sectionProjects[i]);
for each one of the sectionProjects, there is a variable achievements, with an empty array.
Upon sending each sectionProject to the queries.first function, I reassign achievements,
finalObject.sectionProjects[i].achievements = something else
When I return from the queries.first function, I lose the data I added. Am I doing something wrong?
Here's the function:
module.exports = {
    first:function(aProject) {
        // Latest achievements
        var query =
                " SELECT ta.description, ta.remarks, ta.expectedECD " +
                " FROM project pr, task ta, milestone mi " +
                " WHERE pr.ID = mi.project_ID AND mi.ID = ta.milestone_ID " +
                " AND ta.achived = ta.percent AND pr.ID = " + aProject.project_id +
                " ORDER BY pr.expectedECD " +
                " LIMIT 5;"
        ;
        var stringified = null;
        pmdb.getConnection(function(err, connection){
            connection.query(query,  function(err, rows){
                if(err) {
                    throw err;
                }else{
                    var jsonRows = [];
                    for( var i in rows) {       
                        stringified = JSON.stringify(rows[i]); 
                        jsonRows.push(JSON.parse(stringified));
                    }       
                    connection.release();                       
                    aProject.achievements = jsonRows;
                    upcomingTasks(aProject);
                }
            });
        });
    }
}
This is pmdb.js:
var mysql = require("mysql");
var con = mysql.createPool({
  host: "localhost",
  user: "user",
  password: "password",
  database: "database"
});
module.exports = con;
This is the main function that calls queries.first:
// ...Code...
//Number of section projects
var len = jsonRows.length;
console.log("Number of section projects: " + len);
var internal_counter = 0;   
function callbackFun(i){
    (finalObject.sectionProjects[i]).achievements = [];
    queries.first(finalObject.sectionProjects[i]);
    if(++internal_counter === len) {
        response.json(finalObject);
    }
}
var funcs = [];
for (var i = 0; i < len; i++) {
    funcs[i] = callbackFun.bind(this, i);
}
for (var j = 0; j < len; j++) {
    funcs[j]();
}           
 
     
    