0

I am fetching results from a query and want to pass value from result on the next query. Here is my code. But it's not working, I tried setTimeOut function as well.

dbcon.query('SELECT ci_offers.*, ci_advertisers.id as merchant_id, ci_advertisers.full_name, ci_advertisers.phone, ci_advertisers.companylogo FROM ci_offers, ci_advertisers WHERE ci_offers.advertiser_id = ci_advertisers.id AND ci_offers.is_active=1', function(error, results, fields) {

      if (error) throw error;

      if (results.length > 0) {
            var objectOffer;
            var objCoin;
            // async.forEachOf(results, function (dataElement, i, inner_callback){

            results.forEach((val) => {

                objectOffer = jsonParser(val);

                var token_id = objectOffer.token_id;

                var redeem_coin_grabbed = 0;

                if (token_id > 0) {

                  dbcon.query('SELECT count(id) as coingrabbed FROM ci_grabcoin_details_for_game WHERE user_id = ? AND coin_id= 33', [user_id, token_id], function(error1, results1, fields1) {

                    if (error1) throw error1;

                    });


                }
                val["coingrabbed"] = redeem_coin_grabbed;

                final.push(val);
            });
        }

        res.send(final);
    });
2
  • Tell us the exact error you are facing. Commented Oct 15, 2019 at 6:40
  • 1
    What is not working? What do you expect and what do you get? Where (approximately) is the code failing? Commented Oct 15, 2019 at 6:50

1 Answer 1

0

Please check if this works, Using this idea https://stackoverflow.com/a/7053992/9216722

let final = [];

dbcon.query('SELECT ci_offers.*, ci_advertisers.id as merchant_id, ci_advertisers.full_name, ci_advertisers.phone, ci_advertisers.companylogo FROM ci_offers, ci_advertisers WHERE ci_offers.advertiser_id = ci_advertisers.id AND ci_offers.is_active=1', function(error, results, fields) {

  if (error) throw error;

  if (results.length > 0) {
        var objectOffer;
        var objCoin;
        // async.forEachOf(results, function (dataElement, i, inner_callback){

        let done = false;
        let count = 0;
        const markDone = (err) => {
            count++;
            if ( !done && results.length === count) {
                done = true;
                res.send(final);
            }
        };


        results.forEach((val) => {

            objectOffer = jsonParser(val);

            var token_id = objectOffer.token_id;

            var redeem_coin_grabbed = 0;

            if (token_id > 0) {

              dbcon.query('SELECT count(id) as coingrabbed FROM ci_grabcoin_details_for_game WHERE user_id = ? AND coin_id= 33', [user_id, token_id], function(error1, results1, fields1) {

                if (error1) throw error1;

                });


            }
            val["coingrabbed"] = redeem_coin_grabbed;

            final.push(val);

            markDone();

        });
    }

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

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.