matteofigus / nice-json2csv

A node.js tool for converting json to csv
MIT License
39 stars 10 forks source link

TypeError: Object #<ServerResponse> has no method 'csv' #2

Closed nestorvc closed 10 years ago

nestorvc commented 11 years ago

I am using the plugin on Express. As the tutorial for Express shows, I am using the plugin the following way on my controller:

var json2csv = require('nice-json2csv');

//Coming from a GET route
exports.downloadCSV = function(req, res) {
  Model.loadAll(function(err, results) {
    if (err) {...}
    res.csv(results, "results.csv");
  });
};

However, I keep getting this error: TypeError: Object #<ServerResponse> has no method 'csv'

matteofigus commented 11 years ago

I tried to test a little better this functionality, probably the documentation needs to explain a little better how to make this work/I need to make the module work a little better :dancers:

So, try removing from this file var json2csv = require('nice-json2csv');

and include it, instead, in the file where you initiate express and his routes. Basically, immediately after your require('express'), wherever it is. If it works, then I need to update the docs/test this better.

Thanks!

nestorvc commented 11 years ago

Thanks for the reply, but nope. It keeps crashing. I tested an old project that had it implemented correctly and that used to works, but now is not working anymore. I am attaching the full error log:

08:36:12 web.1  | myPath/app/controllers/adminController.js:100
08:36:12 web.1  |       res.csv(results, "participantes.csv");
08:36:12 web.1  |           ^
08:36:12 web.1  | TypeError: Object #<ServerResponse> has no method 'csv'
08:36:12 web.1  |     at Promise.<anonymous> (myPath/app/controllers/adminController.js:100:11)
08:36:12 web.1  |     at Promise.<anonymous> (myPath/node_modules/mongoose/node_modules/mpromise/lib/promise.js:171:8)
08:36:12 web.1  |     at Promise.EventEmitter.emit (events.js:95:17)
08:36:12 web.1  |     at Promise.emit (myPath/node_modules/mongoose/node_modules/mpromise/lib/promise.js:88:38)
08:36:12 web.1  |     at Promise.fulfill (myPath/node_modules/mongoose/node_modules/mpromise/lib/promise.js:101:20)
08:36:12 web.1  |     at Promise.resolve (myPath/node_modules/mongoose/lib/promise.js:108:15)
08:36:12 web.1  |     at Promise.<anonymous> (myPath/node_modules/mongoose/node_modules/mpromise/lib/promise.js:171:8)
08:36:12 web.1  |     at Promise.EventEmitter.emit (events.js:95:17)
08:36:12 web.1  |     at Promise.emit (myPath/node_modules/mongoose/node_modules/mpromise/lib/promise.js:88:38)
08:36:12 web.1  |     at Promise.fulfill (myPath/node_modules/mongoose/node_modules/mpromise/lib/promise.js:101:20)
08:36:12 web.1  | exited with code 8
08:36:12 system | sending SIGTERM to all processes

Another clue could be that I am using always the "latest" version of express on my Package.json. I remember to saw a Warning when installing your plugin on NPM about my Node version compatibility. Could be something related to that?

//Package.json
"dependencies": {
        "express": "latest",
        "jade": "latest",
        "async": "latest",
        "underscore": "latest",
        "mongoose": "latest",
        "mongoose-user": "latest",
        "connect-mongo": "latest",
        "nice-json2csv": "latest"
    },
    "engines": {
        "node": "0.8.x",
        "npm": "1.2.x"
    },
nestorvc commented 11 years ago

I tried changing the Package.json to be just like what your plugin needs, but didn't work.

"dependencies": {
        "express": "3.4.2",
        "jade": "latest",
        "async": "latest",
        "underscore": "latest",
        "mongoose": "latest",
        "mongoose-user": "latest",
        "connect-mongo": "latest",
        "nice-json2csv": "0.0.10"
    },
    "engines": {
        "node": "0.8.x",
        "npm": "1.1.x"
    },
matteofigus commented 11 years ago

Ok, so, I changed the way to integrate it and I issued a version 0.0.11.

Basically, the library was trying to extend the res object but depending on the situation it was kind of tricky and in any case a little bit hacky and not so clean. Not really sure what was happening in your specific scenario as I should see all the rest of the code.

I changed it in a way that I like a little bit more, using the express' app.use() functionality that should be perfect for decorating the Response object and should decrease the chances to fail.

Basically you don't need to require express or nice-json2csv in your route file anymore to use res.csv, the only requirement is to do it where you instantiate express via express(), so basically your middleware, and just there! After that all your website should have the res.csv object available. If necessary, you can take a look at the tests, there is a specific one that tries to emulate your scenario

matteofigus commented 11 years ago

PS - let me know how it goes

nestorvc commented 10 years ago

Hi Matteo. Thanks for the reply and the custom tests... However, it didn't work for me. Does the plugin work right for you right now? I realize that I'm using the latest node version, not the 0.8.x, and I may not downgrade it cause I needed this way for several other projects.

Could be this a reason for my issue? Thanks again for the awesome support.

matteofigus commented 10 years ago

Of course it works, all the test pass on 0.6, 0.8, 0.10 and 0.11. Can you try doing a npm install and then npm test to see if all the test run on your environment?

matteofigus commented 10 years ago

Closing it as I didn't managed to reproduce the error. Happy to help in case you want to share more code. In that case, open a new issue. Thanks