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)
上面是带有 Node.js 请求模块的 Coffeescript。我不知道如何获取请求函数内部的数据。这是我申请的一个主要障碍。
谢谢!
** 编辑**
Vadim Baryshev 的代码更新做到了!非常感谢你:)!
最佳答案
您正尝试在 request
函数回调中分配 exports.inside
之前输出它。因为 request
函数是异步的。您可以通过回调或事件获取此函数的结果。
更新:
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)
关于node.js - 具有请求范围的 CoffeeScript ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13210765/