node.js - 具有请求范围的 CoffeeScript

标签 node.js coffeescript

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/

相关文章:

node.js - 无法在 CENTOS 7 上切换 Node 版本

javascript - Coffeescript 中的 promise 。尝试将多个参数传递给 `unexpected comma` 时出现 `then` 错误?

JavaScript 到 Coffeescript

javascript - Backbone Coffeescript保存成功功能

javascript - Javascript中promise和@@iterator的问题

node.js - Yoeman 与generator-fountain-webapp 发生错误

node.js - 如何在 node.js 中将文件定义为模块

coffeescript - CoffeeScript 上的 Javadoc?

javascript - grunt-contrib-handlebars - 输出与我运行 handlebars npm 任务时不同

node.js - 在 Node.js 中实现密码重置