节点包装器:超出最大调用堆栈大小

时间:2014-02-10 00:01:08

标签: node.js api wrapper stack-overflow

我正在为API构建一个Node包装器,并在将POST请求中的pdf文件附加到服务器时收到堆栈溢出错误。

核心逻辑位于src/lob.coffee

fs = require "fs"

USER_AGENT = "Lob: Node Wrapper"

API_HOST = "https://api.lob.com"
API_PATH = "/v1"

module.exports = lob = (api_key) ->

  request = (method, path, data, cb) ->

    request_object = {
      method : method,
      uri : "#{API_HOST}#{API_PATH}#{path}",
      headers : {
        "Accept" : "application/json",
        "Connection" : "close",
        "User-Agent" : USER_AGENT
      },
      auth : {
        user : api_key,
        pass : ""
      }
    }

    if typeof data is "function"
      cb = data

    r = request request_object, (err, res, body) ->
      body = JSON.parse(body)
      if body.errors? and err is null
        err = body.errors
        delete body.errors
      cb err, body
      this

    if method is "POST_FORM"
      form = r.form()
      Object.keys(data).forEach (_k) ->
        form.append _k, data[_k]
      this

  objects:
    createObject: (data, cb) ->
      path = "/objects"
      data.file = fs.createReadStream data.file.substr 1
      request "POST_FORM", path , data, cb

我在test/manual.coffee中编写了这个基本文件来测试功能:

api_key = "test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc"
lob     = require('../src/lob')(api_key)

object2 =
  name: "TEST"
  file: "@/Users/Admin/Downloads/test.pdf"
  setting_id: 100

lob.objects.createObject object2, (err, res) ->
  console.log err, res

但是当我运行测试文件时,我收到以下错误:ERROR: RangeError: Maximum call stack size exceeded并且我不确定原因是什么。

1 个答案:

答案 0 :(得分:0)

递归

objects:
    createObject: (data, cb//here) ->
      path = "/objects"
      data.file = fs.createReadStream data.file.substr 1
      request "POST_FORM", path , data, cb //here