简体   繁体   English

具有请求范围的coffeescript

[英]coffeescript with Request scoping

request = require('request')

auth =
url: ''
method: 'POST'
json:
  credentials:
  username: ""
  key: ""

exports = exports ? this

request auth, (err, res, body) ->
  exports.inside = body

console.log(exports.inside)


Then above is Coffeescript with the request module for Node.js. 然后上面是带有Node.js请求模块的Coffeescript。 I cannot figure out how to get the data inside of the request function out. 我无法弄清楚如何获取请求函数内部的数据。 This has been a major road-block for my application. 这一直是我应用程序的主要障碍。

Thank you! 谢谢!

** EDIT** **编辑**

Vadim Baryshev's update with code did it! Vadim Baryshev的代码更新做到了! Thank you so much :) ! 非常感谢 :) !

You are trying to output exports.inside before it have been assigned in request function callback. 您正在尝试输出exports.inside然后再在request函数回调中分配它。 Because request function is asynchronous. 因为request功能是异步的。 You can get result of this function via callback or event. 您可以通过回调或事件获取此函数的结果。

Update : 更新

request = require('request')

exports = exports ? this

getAuth = (callback) ->
  auth =
  url: ''
  method: 'POST'
  json:
    credentials:
      username: ""
      key: ""

  request auth, (err, res, body) ->
    exports.inside = body
    callback err, body

getAuth (err, body) ->
  # here is exports.inside ready
  console.log exports.inside
  # also you can access body and request error arguments here
  # body === exports.inside here
  # err is request error (is null/undifined if request is successful)

Once the request function completes, it triggers a callback which is the only place where you can reliably access the value of "body." 请求函数完成后,它将触发一个回调,这是您可以可靠访问“ body”值的唯一位置。

The problem you're having is that, when the console.log function runs, the callback has not been fired because the request is not complete. 您遇到的问题是,当console.log函数运行时,由于请求未完成,因此未触发回调。

See Problems with use fs.stat in nodejs for a more cogent description of program flow within an asynchronous programming environment. 有关异步编程环境中程序流的更详尽的描述,请参阅nodejs中使用fs.stat的问题

---EDIT--- with example: ---编辑---例如:

Consider this: 考虑一下:

1: path='/tmp/file.txt'
2: result=''
3: fs.readFile path, (err,data) ->
4:    throw err if err
5:    result=data
6: console.log result

If we were to trace this operation, we would find that the order of execution would be 1,2,3,6,...4,5 where, due to the nature of disc i/o, the ellipsis represents some unknown amount of time. 如果要跟踪此操作,我们会发现执行顺序为1,2,3,6,... 4,5,其中,由于光盘I / O的特性,省略号表示一些未知量时间。

Because it takes some time for the read operation to complete, rather than wait for its result, we provide a callback function which will be called at some unpredictable point in the future when the contents of the file have been read and so can be assigned to 'result'. 因为读取操作要花一些时间才能完成,而不是等待结果,所以我们提供了一个回调函数,该函数将在将来读取文件内容时在某些不可预测的时刻调用,因此可以将其分配给'结果'。

When program flow reaches line 6, the callback has not been called because the file read operation has not completed, and so the result has not been set. 当程序流到达第6行时,由于文件读取操作尚未完成,因此尚未调用回调,因此未设置结果。

This is the nature of asynchronous programming where rather than waiting for operations to complete before moving on, we can use the wasted time for other purposes. 这就是异步编程的本质,我们可以将浪费的时间用于其他目的,而不是在继续操作之前等待操作完成。

---2ND EDIT--- OK, per your request, here's your example modified so that it works. --- 2ND编辑---好的,根据您的要求,以下示例经过修改,可以正常工作。

request = require('request')

auth =
  url: ''
  method: 'POST'
  json:
    credentials:
    username: ""
    key: ""

exports = exports ? this

request auth, (err, res, body) ->
  exports.inside = body
  console.log(exports.inside)

Note that, as has now been described several times, you cannot access the result of the request outside of the callback because you cannot know when the request will complete. 请注意,正如已经多次描述的那样,您无法在回调之外访问请求的结果,因为您不知道请求何时完成。

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

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