1

Is any possible to pass array into mysql callback like:

connection.query('SELECT count(id) as wynik FROM used where asset_id = 
  \''+asid+'\'', inventory[i],function(err,wiersze){
    console.log(inventory[i]);
});

Now inventory[i] is not avaible inside, but I need to have it.

1
  • What should inventory[i] be?Which mysql module are you using? Looking at the introduction in node-mysql, I'd guess you want connection.query('SELECT count(id) as wynik FROM used where asset_id = \''+asid+'\'', function(err,wiersze) { console.log(wiersze[0].wynik); }); Commented Jan 23, 2016 at 18:49

2 Answers 2

1

Since you're inside a for-loop, you'll need to use an immediately-invoked-anonymous-function or a simple .forEach(). Both are provided as examples below.

Immediately-invoked-anonymous-function

for (var i = 0; i < inventory.length; i++) {
    (function(i) {
        if (inventory[i].market_hash_name == rows[j].real_name) {
            var asid = inventory[i].assetid;
            connection.query('SELECT count(id) as wynik FROM used where asset_id = \'' + asid + '\'', function(err, wiersze) {
                console.log(inventory[i]);
                process.exit(1);
                if (wiersze[0].wynik == 0) {
                    var employee = {
                        asset_id: asid,
                        trans_id: rows[j].tid
                    };
                    connection.query('INSERT INTO used SET ?', employee, function(err, res) {
                        if (err) throw err;
                        offer.addMyItem(inventory[i]);
                    });
                    licznik++;
                }

            });

        }
    })(i);
});

.forEach()

inventory.forEach(function(item) {
    if (item.market_hash_name == rows[j].real_name) {
        var asid = item.assetid;
        connection.query('SELECT count(id) as wynik FROM used where asset_id = \'' + asid + '\'', function(err, wiersze) {
            console.log(item);
            process.exit(1);
            if (wiersze[0].wynik == 0) {
                var employee = {
                    asset_id: asid,
                    trans_id: rows[j].tid
                };
                connection.query('INSERT INTO used SET ?', employee, function(err, res) {
                    if (err) throw err;
                    offer.addMyItem(item);
                });
                licznik++;
            }

        });

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

4 Comments

if I do like this, inventory is not avaible in query block. Return undefinited.
Could you provide a larger code sample? There's something else going on with your code.
Ah, you are inside a for-loop. That's what I thought was happening, but no worries. I've edited my answer with two different solutions for you.
Thanks a lot. But I have 1 more question. How increment var licznik? I try sth like this but it is not working. pastebin.com/raw/KzeWyszf
1

Pass your callback() directly after your string.

Try this :

connection.query("SELECT count(id) as wynik FROM used where asset_id = " + asid, function(err, rows){
     /* check errors here */
     console.log(rows);
 });

Comments

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.