0

I'm using this module: https://github.com/nulltask/express-csv (also tried a couple more)

       MyModel.find(mquery,function(err, list){
            if(err){
                console.log(err);
                var response = {
                    success: false,
                    data: null
                };
                res.json(response);
            }else{
                console.log(list);
                res.setHeader('Content-disposition', 'attachment; filename=mylist.csv');
                res.csv(list);
            }
        });

The problem is what is rendered in the csv file. It is Mongoose's Model itself. But on the console logs the actual data.

Any hints?

Update

Apparently static JSON data it is rendered in the CSV file, so must be something with syncing....

George I.
  • 560
  • 2
  • 7
  • 26
  • How do you see the console logs? from your code it will only console.log if there is an error.. – Johnny Tordgeman Jun 09 '15 at 10:47
  • I left it out. And add it back after edit. – George I. Jun 09 '15 at 11:14
  • Is the csv you are writing complicated or something? It's quite easy to write such csv file on your own without any external library. Check out this fiddle: https://jsfiddle.net/zcsmnzy6/ I created this module for a project i worked on and it outputted data from a mongoose find query without any issues... – Johnny Tordgeman Jun 09 '15 at 11:54
  • I was just writing a similar solution. Unfortunately the result is the same. – George I. Jun 09 '15 at 12:32
  • The way i used the callback is as follows: router.get('/export', function (req, res) { api.getUsers().then(function (result) { var csv = helpers.convertToCsv(result); res.header('content-type', 'text/csv'); res.header('content-disposition', 'attachment; filename=contactus.csv'); res.write(csv); res.end(); }); }) – Johnny Tordgeman Jun 09 '15 at 12:35
  • Thanks @JohnnyTordgeman, but it seems that is something else. I still get the 'Document' API from Mongoose instead the data. – George I. Jun 09 '15 at 12:51
  • What do you mean by 'document' api? can you add to the question the full function code? the output of the csv? – Johnny Tordgeman Jun 09 '15 at 12:53
  • Having the same issue. Especially evident with @JohnnyTordgeman 's fiddle. The mongoose query callback returns a bizarre object that the docs don't mention, right now it is like it's full of ghost keys holding functions that get printed – sclarky Dec 08 '16 at 21:31
  • Oh, not an object, returns Document and that's the problem. http://stackoverflow.com/a/14504387/3366809 – sclarky Dec 08 '16 at 21:38

1 Answers1

0

The mongoose query returns a Document, not a simple object, despite not stating that clearly in their docs (stackoverflow.com/a/14504387/3366809). This confounds express-csv or anything trying to loop over it. Use lean() in the query to return an object instead of a Document.

var myquery = MyModel.find(mquery);
myquery.lean().exec(function(err, list) {
  if (err) {
    console.log(err);
    var response = {
      success : false,
      data : null
    };
    res.json(response);
  } else {
    console.log(list);
    res.setHeader('Content-disposition', 'attachment; filename=mylist.csv');
    res.csv(list);
  }
});
sclarky
  • 721
  • 5
  • 11