简体   繁体   中英

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. 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! Thank you so much :) !

You are trying to output exports.inside before it have been assigned in request function callback. Because request function is asynchronous. 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."

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.

See Problems with use fs.stat in nodejs for a more cogent description of program flow within an asynchronous programming environment.

---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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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