Guest User

Untitled

a guest
Nov 20th, 2013
63
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* This is test function to insert data in the mysql table , I know there are few things wrong , I just need a sollution to run this , the problem is , I will pass a JSON array like this [{tempid:2,email1:abc@123,address:hihere},{{tempid:3,email1:abc@1232,address:hihere2}}] When I am passing only one tempid it works fine but with passing the tempid twice it does not work and says email1 is defined twice , it means the forEach is running without the execution of the querry function is done already. So I thing making the querry sync from async is one sollution , please help ! */
  2. exports.test=function (arr,email,callback)
  3. {var uid;
  4. var datatable=[];
  5. var inserting=[];
  6. var values=[];
  7. var showuser=[];
  8. forEach(arr,function(row,index){
  9. if(arr[index].tempid){
  10. var tmpid=arr[index].tempid;
  11. var pair=[];
  12. var o=arr[index];
  13. pair=Object.keys(o).map(function(a){ return [a, o[a]] });
  14. connection.query('SELECT UID FROM user where email1="'+email.email+'"',function(err,rows,fields){
  15. if(err){
  16. callback(err+"Service Error!");}
  17. else{
  18. if(rows[0]!=undefined){
  19. uid=rows[0]['UID'];
  20. connection.query('SELECT column_name FROM information_schema.columns where table_schema="webservice" AND table_name="usercontacts"',function(err,rows,fields){if(err){
  21. callback(err+" Service Error!")}
  22. else if(rows[0]!=undefined){
  23. for(var j=0;j<=rows.length-1;j++){
  24. datatable.push(rows[j]['column_name']);
  25. }
  26. for(var k=0;k<=pair.length-1;k++){
  27. if(inArray(pair[k][0],datatable)){
  28. inserting.push(pair[k][0]);
  29. values.push('"'+pair[k][1]+'"');
  30. }
  31. }
  32. connection.query('INSERT INTO usercontacts (cid,uid,'+inserting+') VALUES("","'+uid+'",'+values+')',function(err,rows,fields){
  33. if(err){
  34. console.log(inserting+":"+values);
  35. throw err;
  36. callback(err+" Service Error!");}else{
  37. connection.query('SELECT * FROM usercontacts WHERE uid="'+uid+'" ORDER BY cid DESC LIMIT 0,1',function(err,rows,fields){
  38. if(err){
  39. }else if(rows[0]!=undefined){showuser.push("TempId: "+tmpid +" cid: "+rows[0].cid);
  40. showuser.push(inserting+":"+values);
  41. callback(null,showuser);
  42. }else{
  43. callback("No Cid found !");
  44. }
  45. });
  46. } });
  47. }else
  48. {callback(" No such field in database!");}
  49. });
  50. }else{
  51. callback(" No user matched");}
  52. }});// get the UID of the inserting user
  53. }
  54. else if(arr[index].cid)
  55. {
  56. //console.log(arr[i]);
  57. }
  58. else
  59. {callback("pass atleast one tempid or cid");
  60. }
  61. });
  62. }
RAW Paste Data