0

Здравствуйте! Изучаю node.js недавно и возник следующий вопрос. Как можно добиться того, что бы скрипт ждал выполнения первого sql запроса и в зависимости от результата решал, нужно ли запускать второй запрос на добавление или нет. Заранее прошу помидорами не закидывать =) Заранее благодарен за ваши советы.

    var request = require('request');

var mysql = require('mysql');

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : 'root',
  database : 'parser'
});

request({
    url: "https://cs.money/load_bots_inventory?hash=1522257203953",
    method: "GET"

}, function (error, response, body){

     var obj = JSON.parse(body);
     //
     var key="";
     for (var count=0;count<5000;count++) {

        if(typeof obj[count].m != 'undefined' ){    
            key = obj[count].m+obj[count].e;//obj[count].p
            if(key.indexOf(" ")==1){
                key = key.substring(2,key.length);
            }
            var query = "select * from item where keyword = "+mysql.escape(key);

            connection.query(query, function(error, result, fields){
                 if (error){
                    throw error;
                 } 
                 else{
                    if(typeof result[count] != 'undefined' ){   

                    }
                    else{

                        connection.query('INSERT INTO item SET ?', {name: obj[count].m ,exterior:obj[count].e,keyword:key}, 
                            function(error, result, fields){
                                console.log(count);

                        });
                    }


                 }

            });

        }
        else{
            break;
        }
    }

});



//connection.end();
/*request({
    url: "https://skinsjar.com/api/v3/load/bots?refresh=1&v=0",
    method: "GET"

}, function (error, response, body){

    var obj = JSON.parse(body);
    console.log(typeof(obj.items));
    var count=0;
    for (var ob in obj.items) {
        count++;
        if(typeof obj.items[count] != 'undefined' ){    
            console.log(obj.items[count].name + "\n" + obj.items[count].price + "\n");
            break;
        }
    }

});*/

/*request({
    url: "https://cs.money/load_bots_inventory?hash=1522257203953",
    method: "GET"

}, function (error, response, body){

    //var obj = JSON.parse(response);
     console.log(body);
});*/
Vadim
  • 115
  • 1
  • 10

0 Answers0