Search code examples
node.jscoffeescriptexpressform-post

Why does an HTTP request in Node.js yield the data type as well?


  req = http.request options, (res) ->
    res.on 'data', (chunk) ->
      data += chunk
      return
    res.on 'end', ->
      if res.statusCode is 200
        console.log res
        console.log data
        callback null, data
      else
        callback(new Error("Response status code: " + res.statusCode), data)
      return
    return

When I output data, I get: [object Object][["INZ", 41.878113599999999, -87.629798199999996]] which is odd because the part after [object Object] is all good. Any ideas?


Solution

  • You're not defining data, so you're getting undefined += chunk, as I said in my comment. You can solve it like @Trevor Burnham said:

    req = http.request options, (res) ->
      data = ''
      res.on 'data', (chunk) ->
        data += chunk
        return
    ...