Q:

Node JS for loop inserting data twice

I am trying import the contacts in a database but sometime its inserting records 2 times. Requirement is, If the number is already exist it should get updated otherwise inserted as a new row. I am using MySQL database.
I am using for loop with async.
var numbers = {
    numbers:[
        {
            name:"A",
            number:9876543211
        },
        {
            name:"B",
            number:7876543211
        },
        {
            name:"C",
            number:9886543211
        },
        {
            name:"D",
            number:8876543211
        }
    ]
};
async.forEachOf(numbers, (numberObj, key, callback) => {
    var createdAt = moment.utc().valueOf();
    var updatedAt = moment.utc().valueOf();
        gfs.checkContact(userInfo.user_id, code, numberObj.number, function(contactInfo, err){
        if(err){
            response.error = "sorry";
            res.send(response); return false;
        }else{
            if (contactInfo.length > 0) {
                gfs.qry("UPDATE contacts SET fullName='"+numberObj.name+"', updatedAt='"+updatedAt+"' WHERE cid='"+contactInfo[0].cid+"'").then(function (results){
                }).catch(function (errorMessage){
                })
            }else{
                gfs.qry("INSERT INTO contacts(user_id, fullName, code, mobile, createdAt, updatedAt) VALUES('"+userInfo.user_id+"', '"+numberObj.name+"', '"+code+"', '"+numberObj.number+"', '"+createdAt+"', '"+updatedAt+"')").then(function (results){
                }).catch(function (errorMessage){
                })
            }
        }
            callback();
        });
    }, err => {
    if (err){
        response.error = "sorry";
        res.send(response);
    }else{
        response.success = "success";
        response.numbers = numbers;
        res.send(response);
    }
});

I want to insert the contact number if it's not exist in the database for logged-in user id or it should get update the other fields like name, updated at if number already in database for the logged-in user id.

A:

I have changed my code, now using promise. It's working fine now.
var numbers = [
{

    name:"A",

    number:9876543211

},

{

    name:"B",

    number:7876543211

},

{

    name:"C",

    number:9886543211

},

{

    name:"D",

    number:8876543211

}

];
async.forEachOf(numbers, (numberObj, key, callback) => {
var createdAt = moment.utc().valueOf();

var updatedAt = moment.utc().valueOf();

gfs.checkContactPromise({

    user_id:userInfo.user_id,

    code:code,

    mobile:numberObj.number,

    fullName:numberObj.name,

    createdAt:createdAt,

    updatedAt:updatedAt

}).then( function (addContactQry){

    gfs.qry(addContactQry).then(function (results){

        userContacts.push("'"+numberObj.number+"'");

        callback();

    }).catch(function (errorMessage){

        callback();

    })

}).catch( function (errorMessage){

    callback();

});

}, err => {
if (err){

    response.error = "sorry";

    res.send(response);

}else{

    response.success = "success";

    response.numbers = numbers;

    res.send(response);

}

});

