Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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 ! */
- exports.test=function (arr,email,callback)
- {var uid;
- var datatable=[];
- var inserting=[];
- var values=[];
- var showuser=[];
- forEach(arr,function(row,index){
- if(arr[index].tempid){
- var tmpid=arr[index].tempid;
- var pair=[];
- var o=arr[index];
- pair=Object.keys(o).map(function(a){ return [a, o[a]] });
- connection.query('SELECT UID FROM user where email1="'+email.email+'"',function(err,rows,fields){
- if(err){
- callback(err+"Service Error!");}
- else{
- if(rows[0]!=undefined){
- uid=rows[0]['UID'];
- connection.query('SELECT column_name FROM information_schema.columns where table_schema="webservice" AND table_name="usercontacts"',function(err,rows,fields){if(err){
- callback(err+" Service Error!")}
- else if(rows[0]!=undefined){
- for(var j=0;j<=rows.length-1;j++){
- datatable.push(rows[j]['column_name']);
- }
- for(var k=0;k<=pair.length-1;k++){
- if(inArray(pair[k][0],datatable)){
- inserting.push(pair[k][0]);
- values.push('"'+pair[k][1]+'"');
- }
- }
- connection.query('INSERT INTO usercontacts (cid,uid,'+inserting+') VALUES("","'+uid+'",'+values+')',function(err,rows,fields){
- if(err){
- console.log(inserting+":"+values);
- throw err;
- callback(err+" Service Error!");}else{
- connection.query('SELECT * FROM usercontacts WHERE uid="'+uid+'" ORDER BY cid DESC LIMIT 0,1',function(err,rows,fields){
- if(err){
- }else if(rows[0]!=undefined){showuser.push("TempId: "+tmpid +" cid: "+rows[0].cid);
- showuser.push(inserting+":"+values);
- callback(null,showuser);
- }else{
- callback("No Cid found !");
- }
- });
- } });
- }else
- {callback(" No such field in database!");}
- });
- }else{
- callback(" No user matched");}
- }});// get the UID of the inserting user
- }
- else if(arr[index].cid)
- {
- //console.log(arr[i]);
- }
- else
- {callback("pass atleast one tempid or cid");
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement