2

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]();
}           
Community
  • 1
  • 1
Abu3odeh
  • 73
  • 6

1 Answers1

1

Read That Answer twice. Objects acts as a wrapper for the scalar primitive property. You are passing the Objects in to the "queries.first" function.

See this Object reference issue

Edited for the sample code

    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)
            }
        });
    });

that is not a problem. change it like this. "upcomingTasks" is not a callback function. it is execute after assign the achievements in aProject

Community
  • 1
  • 1
Anbu
  • 490
  • 6
  • 20
  • I'm changing my variable using the dot '.' operator, which follows the 'Object reference issue' link, and I'm applying it obj1 in the first link, so can you tell me what it is I should look at? – Abu3odeh Mar 28 '16 at 06:42
  • I changed it, but it's still not changing. I'll add code from the main function – Abu3odeh Mar 28 '16 at 07:39