0

I.m newbie in nodejs and have some problem , with returning value to main function.

i try something like this but dosent work for my.

function request_price(name)
{

    var price;

    (function(price_r) {

        request('http://jack.dev/price.php?name='+encodeURIComponent(name), function(error, response, body)
        { 

            console.log(body);
            price_r = body;

        });

    })(price);

    return price;

}

I need return body value from request to main function request_price

Edit:

for (var i = 0; i < offer.items_to_receive.length; i++) {

    for (var j = 0; j < inventory.length; j++) {

        if (offer.items_to_receive[i].assetid == inventory[j].id) {

            var price = request_price(inventory[j].market_name, responseHandler);

            OfferData.items.push({

                id: inventory[j].id,
                name: inventory[j].name,
                image: inventory[j].icon_url,
                price: price

            });

            break;

        }

    }

}

setTimeout(function () {
    console.log(OfferData);
}, 1000)

responseHandler function showing console.log fine , but object OfferData on console.log return undefined on price

Roco CTZ
  • 1,107
  • 1
  • 16
  • 31
feesar
  • 480
  • 2
  • 5
  • 21
  • 1
    There are hundreds of dups for this question. It's ***async***! You can't return the value from your function. You must use a callback. I've marked it a dup of [How to return response from an asynchronous call](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call). While that specific question is about client-side code, the concept is identical and that answer covers the various options better than others. – jfriend00 Aug 27 '15 at 01:25
  • Duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – jfriend00 Aug 27 '15 at 01:28
  • with jquery can use proxy , to solve this problem , but in node i dont know. – feesar Aug 27 '15 at 01:30
  • Read the selected answer to that question. It covers multiple options and explains the concept of asynchronous responses. Read, study, learn. Then, ask a more specific question if you don't understand something. You can use promises in node.js just fine if that's what you meant when you said proxy. This question is asked multiple times per day for node.js. It is a very common obstacle for people learning node.js. It must be studied and learned. – jfriend00 Aug 27 '15 at 01:32
  • 1
    I have edited my original post with more information. It is just a suggestion on how to resolve your problem. As @jfriend00 was saying, this is a very common skill that you have to master in nodejs. – Roco CTZ Aug 27 '15 at 01:42

1 Answers1

1

It doesn't work the way you tried it.

What you try to do is to call a request_price() and inside you are making a request call (which should be request.get(...) if you are using the request library );

The request() function runs asynchronously so it might take a while until it's resolved, but the code doesn't wait so it moves forward to return price; which doesn't have a value yet.

By the time the response from the request() function comes back, the request_price() function has already finished running so this is the reason why it's not working.

In order to make it work, you need to process your price in the request() function callback.

You can have a look at this suggestion that I wrote which might be (IMO) a cleaner implementation:

var request = require('request');

function request_price(name, responseHandler) {
    var price;
    request.get('http://jack.dev/price.php?name='+encodeURIComponent(name), responseHandler );
}

function responseHandler(error, response, body) {
    console.log(body);
    var x = ... //process body
    return x;
}

request_price('bob', responseHandler);

Further edit:

In this case you've just added, you need to follow this logic:

  1. change the responseHandler function so it can perform the OfferData.items.push()
  2. Use the loop to call request_price() as many times as you like
  3. Pass the inventory[j] object to the request_price() function as an argument
  4. When the response comes back and it's processed by responseHandler, call OfferData.items.push() from the inside of responseHandler because now both price and inventory[j] are available
Roco CTZ
  • 1,107
  • 1
  • 16
  • 31
  • work fine , but now have other problem, i have loop thaht foreach some data , inside loop i define variable with value request_price.... and then that variable push into object , on loop end have console.log and value of variable are undefined , but console.log of function shows return value. – feesar Aug 27 '15 at 00:26
  • 1
    Can you share the code that you are talking about so we can see exactly what happens? Probably it's showing undefined for the same reason. The forEach loop needs to be in the callback function. – Roco CTZ Aug 27 '15 at 01:08
  • Why `return body;` in the `responseHandler()`? That isn't doing anything useful and makes the code look like `request_price()` is going to return the `body` which it is not. – jfriend00 Aug 27 '15 at 01:46
  • It's not a "ready-to-use" example, it's just an idea as the OP needs to processes the body to extract the information that he needs before returning a value. Just edited now. – Roco CTZ Aug 27 '15 at 01:52