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]();
} 
asked Mar 28, 2016 at 5:59
4
  • 1
    You need to share queries.first, how is it implemented, is it an async one etc Commented Mar 28, 2016 at 6:01
  • post your function which is assigning the achievements. So I can debug. Also make fiddle Commented Mar 28, 2016 at 6:07
  • your function seems ok. post fiddle to debug your code. or post complete code for better understanding. Commented Mar 28, 2016 at 6:43
  • is this pmdb.getConnection async function ? Commented Mar 28, 2016 at 7:00

1 Answer 1

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

answered Mar 28, 2016 at 6:36
Sign up to request clarification or add additional context in comments.

2 Comments

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?
I changed it, but it's still not changing. I'll add code from the main function

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.