Monday, 5 June 2017

Node JS - mysql - mysql query exec before first done

I have problem bacause i want to first mysql query dont wait for second in callback.

How i want to work :

First function PolaczItemy return mysql id. Second function change this row .

And again.

Now when i run that this taking the same item. But i dont know why ;(

My code is here :

 function Polaczitemy2(hash_name, callback) {
 PolaczItemy(hash_name,function(err,data){
    if(err)
    {
        callback(err,null);
    }else
    {
        console.log('First one time');
        if(data.length > 0)
        {
            if(data)
            {
                ZmienStanNasze(data[0].assetid,2,function(err,data2){
                    if(err)
                    {
                        callback(err,null);
                    }else
                    {
                        console.log('Second one time');
                        callback(null,data);
                    }
                })
            }
        }
    }
 });
}
function PolaczItemy(hash_name, callback) {
    connection.query('select assetid from nasze_itemy where market_hash_name like ? and  wyplacony = 0 limit 1', [hash_name], function (err, rows, fields) {
        if (err)
            callback(err,null);
        else {
            callback(null,rows)
        }
    });
}

function ZmienStanNasze(assetid, stan, callback) {
    connection.query("update nasze_itemy set wyplacony = ?  where assetid = ?", [stan, assetid], function (err, rows, fields) {
        if (err)
            callback(err, null);
        else {
            callback(null, 'Dodano');
        }
    });

}

And im using this function there :

 for (let iii = 0; iii < itemy_przeslane.length; iii++) {
                                                                            var splitted_hash = itemy_przeslane[iii].item_hash_name.split('(');
                                                                            var item_hash_name = '%' + splitted_hash[0] + '%';
                                                                            Polaczitemy2(item_hash_name, function (err, data4) {
                                                                                if (err) {
                                                                                    console.log('Error : ', err);
                                                                                } else {

                                                                                    console.log(data4);



                                                                                }
                                                                            });

Console prints : enter image description here }



via Lukines

No comments:

Post a Comment