1
votes

I am using node.js,express and postgres db for my project. I have few queries in async function which I am not able to execute in a sequence. sample code for reference.

for(let i=0;i<person_size;i++)
{
    var check_person_query="select per_id_pk from person_tbl where per_fname='"+person_fname[i]+"' and per_lname='"+person_lname[i]+"'";

    dbClient.query(check_person_query,function(err,result){
        if(result.rows.length>0)
        {
            console.log("Call: 1.1");
            console.log("person already exists");
        }
        else
        {
            var insert_person_query = "insert into person_tbl (per_fname,per_lname,per_gender,profile_photo) values('"+person_fname[i]+"','"+person_lname[i]+"','"+person_gender[i]+"','profile_photo_link')";
            //console.log("query2: "+insert_person_query);
            dbClient.query(insert_person_query,function(err,result){
                    if (err) throw err;
                    console.log("Call: 1.2");
                    console.log("New person has been added");       
            });

            var fullname = person_fname[i].concat(person_lname[i]);
            low_fullname = fullname.toLowerCase();

            person_pic[i].mv("/home/aniket/content_info/images/"+low_fullname+".jpg", function(err){
                if (err) throw err;
            });                                             
        }
    });
}//end for 

In above code, when if condition fails, the else part gets executed in the very end of my async.series function. As long as if condition succeeds, everything works perfectly in sequential manner. How to sequentially run nested db queries? I have used async.waterfall but still no expected output.

UPDATE: (SOLVED)

Instead of using nested query functions, I removed the nesting, and prepared a single query so that I would obtain an expected result and maintain the execution sequence.

2
You can always use promises and async/await, the latter naturally works with loops. - Estus Flask

2 Answers

0
votes

You should really just use async/await. The pg lib supports promises, so you can just do this:

for(let i=0;i<person_size;i++) {
  const result = await dbClient.query(check_person_query);
  if(result.rows.length <= 0) {
    await dbClient.query(insert_person_query);
  }
}

Here's some more info on using for loops with async/await.

0
votes

Try this, I have just moved image handling code inside the callback of query function, so it should run sequentially.

for(let i=0;i<person_size;i++)
    {
        var check_person_query="select per_id_pk from person_tbl where per_fname='"+person_fname[i]+"' and per_lname='"+person_lname[i]+"'";

        dbClient.query(check_person_query,function(err,result){
        if(result.rows.length>0)
        {
            console.log("Call: 1.1");
            console.log("person already exists");
        }
        else
        {
            var insert_person_query = "insert into person_tbl (per_fname,per_lname,per_gender,profile_photo) values('"+person_fname[i]+"','"+person_lname[i]+"','"+person_gender[i]+"','profile_photo_link')";
            //console.log("query2: "+insert_person_query);
            dbClient.query(insert_person_query,function(err,result){
            if (err) throw err;
            console.log("Call: 1.2");
            console.log("New person has been added");

            var fullname = person_fname[i].concat(person_lname[i]);
            low_fullname = fullname.toLowerCase();

            person_pic[i].mv("/home/aniket/content_info/images/"+low_fullname+".jpg", function(err){
                if (err) throw err;
            });
        });

        }
    });
    }//end for