MEAN Angular 4堆栈从服务器到客户端下载csv文件

时间:2018-04-04 04:37:33

标签: node.js angular express mean-stack

这是我从Angular 4项目调用的NodeJs POST函数。

var generateReport = function(req, res){
    const Json2csvParser = require('json2csv').Parser;
    connection((db) => {
          const fields = ['Gender', 'Name', 'Email'];
          myReport = [];
          db.collection('members_'+req.user._id).find()
          .toArray()
          .then(function(members){
            members.forEach(member => {
                db.collection('plans_'+req.user._id).findOne()
                .then(function(plan){
                    this.myReport.push({
                        "Gender" : member.gender,
                        "Name" : member.name,
                        "Email" : member.email
                    })
                    const json2csvParser = new Json2csvParser({ fields });
                    const csv = json2csvParser.parse(myReport);
                    var fs = require('fs');
                    res.status(200).send(new Buffer(csv));
                    /*fs.writeFile('csv.csv', csv, function (err) {
                      if (err) throw err;
                      res.download('csv.csv');
                    });*/
                })
                .catch(function(err){
                    console.log(err);
                })
            })
          }) 
    });
}

我收到的错误是:

 Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
 at validateHeader (_http_outgoing.js:503:11)
 at ServerResponse.setHeader (_http_outgoing.js:510:3)
 at ServerResponse.header (/home/ubuntu/alpeshvasani/server/node_modules/expr                                                                               ess/lib/response.js:767:10)
 at ServerResponse.send (/home/ubuntu/alpeshvasani/server/node_modules/expres                                                                             s/lib/response.js:194:10)
 at /home/ubuntu/alpeshvasani/server/controllers/data-controller.js:1959:22
 at <anonymous>
 at process._tickCallback (internal/process/next_tick.js:160:7)
 Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the cli                                                                             ent
 at validateHeader (_http_outgoing.js:503:11)
 at ServerResponse.setHeader (_http_outgoing.js:510:3)
 at ServerResponse.header (/home/ubuntu/alpeshvasani/server/node_modules/expr                                                                             ess/lib/response.js:767:10)
 at ServerResponse.send (/home/ubuntu/alpeshvasani/server/node_modules/expres                                                                             s/lib/response.js:194:10)
 at /home/ubuntu/alpeshvasani/server/controllers/data-controller.js:1959:22
 at <anonymous>
 at process._tickCallback (internal/process/next_tick.js:160:7)
 Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the cli                                                                             ent
 at validateHeader (_http_outgoing.js:503:11)
 at ServerResponse.setHeader (_http_outgoing.js:510:3)
 at ServerResponse.header (/home/ubuntu/alpeshvasani/server/node_modules/expr                                                                             ess/lib/response.js:767:10)
 at ServerResponse.send (/home/ubuntu/alpeshvasani/server/node_modules/expres                                                                             s/lib/response.js:194:10)
 at /home/ubuntu/alpeshvasani/server/controllers/data-controller.js:1959:22 at <anonymous>
 at process._tickCallback (internal/process/next_tick.js:160:7)

我曾尝试过其他方式发送 csv ,但它在角度方面失败了..它发送httperror无法解析 json ,因为它正在采取默认情况下以json形式响应。所以我把它改成了{ responseType: 'text' }但是没有运气。

0 个答案:

没有答案