简体   繁体   English

nodejs返回主函数

[英]nodejs return to main function

Im newbie in nodejs and have some problem , with returning value to main function. 我在nodejs中是新手,并且在将值返回给main函数时遇到了一些问题。

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 我需要从请求返回主体值到主函数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 responseHandler函数显示console.log很好,但console.log上的对象OfferData返回价格未定义

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 ); 您尝试做的是调用request_price()然后在内部进行request调用(如果使用请求库 ,则应为request.get(...) );

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; request()函数异步运行,因此它可能需要一段时间才能解决,但是代码没有等待,因此它继续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. 等到request()函数的响应返回时, request_price()函数已经完成运行,因此这就是它不起作用的原因。

In order to make it work, you need to process your price in the request() function callback. 为了使其正常工作,您需要在request()函数回调中处理price

You can have a look at this suggestion that I wrote which might be ( IMO ) a cleaner implementation: 您可以看一下我写的这个建议 ,它可能是( IMO )更干净的实现:

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() 更改responseHandler函数,使其可以执行OfferData.items.push()
  2. Use the loop to call request_price() as many times as you like 使用循环根据需要多次调用request_price()
  3. Pass the inventory[j] object to the request_price() function as an argument inventory[j]对象作为参数传递给request_price()函数
  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 当响应返回并由responseHandler处理时,请从responseHandler内部调用OfferData.items.push() ,因为现在priceinventory[j]都可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM