【问题标题】:nodejs return to main functionnodejs返回主函数
【发布时间】:2015-08-26 23:05:21
【问题描述】:

我是nodejs的新手,有一些问题,返回值到主函数。

我尝试了类似的方法,但对我的工作无效。

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;

}

我需要从请求返回主体值到主函数request_price

编辑:

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 函数显示 console.log 正常,但 console.log 上的对象 OfferData 在价格上返回 undefined

【问题讨论】:

  • 这个问题有数百个重复。这是异步!你不能从你的函数中返回值。您必须使用回调。我已将其标记为How to return response from an asynchronous call。虽然该特定问题是关于客户端代码的,但概念是相同的,并且该答案比其他答案更好地涵盖了各种选项。
  • with jquery 可以使用代理,来解决这个问题,但在节点我不知道。
  • 阅读该问题的选定答案。它涵盖了多个选项并解释了异步响应的概念。阅读,学习,学习。然后,如果您不了解某些内容,请提出更具体的问题。如果这就是您所说的代理时的意思,您可以在 node.js 中使用 Promise。对于 node.js,这个问题每天都会被问多次。对于学习 node.js 的人来说,这是一个非常常见的障碍。它必须被研究和学习。
  • 我已经用更多信息编辑了我的原始帖子。这只是关于如何解决您的问题的建议。正如@jfriend00 所说,这是你在nodejs 中必须掌握的非常常见的技能。

标签: node.js function callback return


【解决方案1】:

它不像你尝试的那样工作。

您尝试做的是调用request_price(),然后在内部进行request 调用(如果您使用的是request library,则应该是request.get(...));

request() 函数异步运行,因此可能需要一段时间才能解决,但代码不会等待,因此它会前进到尚无值的 return price;

request() 函数的响应返回时,request_price() 函数已经运行完毕,所以这就是它不工作的原因。

为了使其工作,您需要在request() 函数回调中处理您的price

你可以看看我写的这个suggestion,它可能是(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);

进一步编辑:

在这种情况下你刚刚添加,你需要遵循这个逻辑:

  1. 更改responseHandler 函数,使其可以执行OfferData.items.push()
  2. 使用循环调用request_price() 次数不限
  3. inventory[j] 对象作为参数传递给request_price() 函数
  4. 当响应返回并由responseHandler 处理时,从responseHandler 内部调用OfferData.items.push(),因为现在priceinventory[j] 都可用

【讨论】:

  • 工作正常,但现在有其他问题,我有循环 thaht foreach 一些数据,在循环内我用值 request_price.... .log 和变量的值未定义,但函数的 console.log 显示返回值。
  • 您能否分享您正在谈论的代码,以便我们确切了解发生了什么?可能出于同样的原因它显示未定义。 forEach 循环需要在回调函数中。
  • 为什么return body;responseHandler() 中?这并没有做任何有用的事情,并且使代码看起来像 request_price() 将返回 body 而它不是。
  • 这不是一个“即用型”示例,这只是一个想法,因为 OP 在返回值之前需要处理主体以提取他需要的信息。现在刚刚编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 2019-07-25
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多