{"action":"created","issue":{"url":"https://api.github.com/repos/nodejs/help/issues/138","repository_url":"https://api.github.com/repos/nodejs/help","labels_url":"https://api.github.com/repos/nodejs/help/issues/138/labels{/name}","comments_url":"https://api.github.com/repos/nodejs/help/issues/138/comments","events_url":"https://api.github.com/repos/nodejs/help/issues/138/events","html_url":"https://github.com/nodejs/help/issues/138","id":146962920,"number":138,"title":"MySQL and Asynchronous, hard to understand !","user":{"login":"flexbrane","id":14235706,"avatar_url":"https://avatars.githubusercontent.com/u/14235706?v=3","gravatar_id":"","url":"https://api.github.com/users/flexbrane","html_url":"https://github.com/flexbrane","followers_url":"https://api.github.com/users/flexbrane/followers","following_url":"https://api.github.com/users/flexbrane/following{/other_user}","gists_url":"https://api.github.com/users/flexbrane/gists{/gist_id}","starred_url":"https://api.github.com/users/flexbrane/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flexbrane/subscriptions","organizations_url":"https://api.github.com/users/flexbrane/orgs","repos_url":"https://api.github.com/users/flexbrane/repos","events_url":"https://api.github.com/users/flexbrane/events{/privacy}","received_events_url":"https://api.github.com/users/flexbrane/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2016-04-08T15:38:51Z","updated_at":"2016-04-12T15:42:39Z","closed_at":null,"body":"Hello,\r\n\r\nI'm starting a project with nodejs, and I have so trouble with this asynchronous system.\r\n\r\nI understand that I must use callbacks to _control_ and have an execution order. But some times It seems that I cannot control some executions and it create me problems !\r\n\r\nFor exemple :\r\n* I have a json file with lot of information about hardware. I can access to information that I need with :\r\n```js\r\nvar fs = require('fs');\r\n  try{\r\n    var file_content = fs.readFileSync(file);\r\n    var json = JSON.parse(file_content);\r\n  }catch(e){\r\n    console.log('\\t [Cannot access to json file \"'+file+'\"!]\\n\\t'+e+'\\n');\r\n    callback_json2db(-1);\r\n  }\r\n```\r\n\r\n* After that, I want to query my MySQL database for **every** json entry (I use a _for_ loop) to know if it already exist,\r\n- If not, I want to insert it into my database\r\n- if yes, I do nothing.\r\n\r\nBut It seems that because of asynchronous nodejs system, I cannot control the order of execution between the two last point that I just explain. \r\nI put part of my code here, I hope you will explain to me what is my mistake :)\r\n\r\n### json2db function\r\n```js\r\nvar fs = require('fs');\r\nvar sql = require('./sql.js');\r\n\r\n\r\nmodule.exports.json2db = function(connection, file, callback_json2db){ // to call this function from another file\r\n\r\n\r\n  try{\r\n    var file_content = fs.readFileSync(file);\r\n    var json = JSON.parse(file_content);\r\n  }catch(e){\r\n    console.log('\\t [Cannot access to json file \"'+file+'\"!]\\n\\t'+e+'\\n');\r\n    callback_json2db(-1);\r\n  }\r\n\r\n  insert_json_db(connection, json, function(toInsert){\r\n    if(toInsert == -1){\r\n      callback_json2db(-1);\r\n    }else{\r\n      json_insert(connection, toInsert, function(isJsonInsertOk){\r\n\r\n      });\r\n    }\r\n  });\r\n\r\n  callback_json2db(0);\r\n}\r\n```\r\n\r\n### json_insert function\r\n```js\r\nfunction insert_json_db(connection, json, isInsertOk){\r\n  for(var i = 0; json[i] != null;i++) {\r\n    // console.log('Compte : ' + json[i].Compte);\r\n    // console.log('\\n----------------------------\\n');\r\n\r\n    connection.query(\"\\\r\n    SELECT id_product FROM product\\\r\n    WHERE compte='\"+json[i].Compte+\r\n    \"' AND name='\"+json[i].Product+\r\n    \"';\",\r\n    function(error, results){\r\n      if(error){\r\n        console.log('\\n\\t[json2db] Query error:\\n\\t'+error);\r\n        isInsertOk(-1);\r\n        return;\r\n      }else{\r\n        if(results[0]){\r\n          //- Product already exist in database\r\n          console.log('Already exist in database');\r\n        }else{\r\n          console.log('No results.');\r\n           //\r\n           // HERE, IF I WANT TO DO AN INSERT QUERY USING THE SAME SYSTEM\r\n           // THAN ABOVE, IT DIDNT WORK  \r\n           // \r\n        }\r\n      }// if no results\r\n    }); // select query\r\n  }// for\r\n\r\n  isInsertOk(0);\r\n  }\r\n```"},"comment":{"url":"https://api.github.com/repos/nodejs/help/issues/comments/208968986","html_url":"https://github.com/nodejs/help/issues/138#issuecomment-208968986","issue_url":"https://api.github.com/repos/nodejs/help/issues/138","id":208968986,"user":{"login":"lordKnighton","id":1773260,"avatar_url":"https://avatars.githubusercontent.com/u/1773260?v=3","gravatar_id":"","url":"https://api.github.com/users/lordKnighton","html_url":"https://github.com/lordKnighton","followers_url":"https://api.github.com/users/lordKnighton/followers","following_url":"https://api.github.com/users/lordKnighton/following{/other_user}","gists_url":"https://api.github.com/users/lordKnighton/gists{/gist_id}","starred_url":"https://api.github.com/users/lordKnighton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lordKnighton/subscriptions","organizations_url":"https://api.github.com/users/lordKnighton/orgs","repos_url":"https://api.github.com/users/lordKnighton/repos","events_url":"https://api.github.com/users/lordKnighton/events{/privacy}","received_events_url":"https://api.github.com/users/lordKnighton/received_events","type":"User","site_admin":false},"created_at":"2016-04-12T15:42:39Z","updated_at":"2016-04-12T15:42:39Z","body":"Glad you found your solution, sorry i couldn't be enough help, i got busy with my own nodejs problems. \r\nanyway be sure to close your issue :) happy hunting "}}