1

I want delete from box_property when all update query finished

I write this code but I'm not sure that this is correct or not.I want run update query synchronous

var data = [1, 2, 3, 4]; //data generate dynamicly  
for (var i = 0; i < BoxData.length; i++) {
    pool.connect(function(err, client, done) {
        client.query("update   box set gamer_id=null where  box_id=$1; ", [data[i]], function(err, resultUpdate) {
            if ((i + 1) == BoxData.length) {
                //when all query finished then run this query
                client.query("delete from box_property where gamer_id=$1;", [gamer_id], function(err, resultUpdate) {})
            }
        })
    })
}

Is there way that run update query sync and after for loop I run delete query?

like this

var data = [1, 2, 3, 4]; //data generate dynamicly  
for (var i = 0; i < BoxData.length; i++) {
    pool.connect(function(err, client, done) {
        client.query("update   box set gamer_id=null where  box_id=$1; ", [data[i]], function(err, resultUpdate) {
        })
    })
}

//when all query finished then run this query
client.query("delete from box_property where gamer_id=$1;", [gamer_id], function(err, resultUpdate) {})

2 Answers 2

1

The same logic, implemented the right way, with pg-promise:

var data = [1, 2, 3, 4]; //data generated dynamically

db.tx(t => {
    return t.none('UPDATE box SET gamer_id = null WHERE box_id IN ($1:csv)', [data])
        .then(() => {
            return t.none('DELETE FROM box_property WHERE gamer_id = $1', [gamer_id]);
        });
})
    .then(() => {
        // success
    })
    .catch(error => {
        // error
    });

For one thing, you do not need to do a loop for that type of update, a single WHERE IN will do. And for another, such sequence of changes should be inside a transaction. The example above shows you how to do both at the same time.

Sign up to request clarification or add additional context in comments.

Comments

1

You should avoid making anything synchronous in NodeJS.

In your case, a simple IN statement should solve that:

var params = [];
for (var i = 1; i <= data.length; i++) {
   params.push("$"+i);
}
 pool.connect(function(err, client, done) {
    client.query("update box " + 
                 " set gamer_id=null " + 
                 " where  box_id in "+params.join(",")+"; ", 
                 [...data], function(err, resultUpdate) {
        client.query("delete from box_property " + 
                     " where gamer_id=$1;", 
                     [gamer_id], function(err, resultUpdate) {})
        })
    })

2 Comments

this query is for example ,is there way that run query sync?
There's no synchronous query in NodeJS. If you need for one action to happen when N other actions complete, either use promises or count callbacks.

Your Answer

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.