REST API调用只能运行一次

时间:2016-02-29 08:25:53

标签: javascript node.js mongodb rest asynchronous

我编写了一个包含许多函数的Nodejs REST API应用程序。但是,当我使用其中一个API时,它会返回它应该返回的excel文件,一切都很好,直到那里。之后,应用程序停止响应,所有其他功能停止工作。有问题的功能的cde如下:

    app.get('/api/Export', function (req, res) {

    UsageData.aggregate([{
                $match: {
                    $and: [{
                        GroupName: {
                            $ne: "Group1"
                        }
                    }, {
                        GroupName: {
                            $ne: "Group2"
                        }
                    }]
                }
            }
            , {
                $group: {
                    _id: '$ToolType'
                    , count: {
                        $sum: 1
                    }
                }
            }]
        , function (err, usagedata) {
            usagedata = getJSON(usagedata);
            // if there is an error retrieving, send the error. nothing after res.send(err) will execute
            if (err)
                res.send(err)


            var wb = new xl.WorkBook();
            var ws = wb.WorkSheet('Usage');
            ws.Column(2).Width(35);
            var headercell1 = ws.Cell(2, 2);
            headercell1.String('Tool');
            var headercell2 = ws.Cell(2, 3);
            headercell2.String('Usage');
            var myStyle = wb.Style();
            myStyle.Font.Bold();
            myStyle.Fill.Color('#CCCCCC');
            myStyle.Border({
                top: {
                    style: 'thick'
                    , color: '000000'
                }
                , right: {
                    style: 'thick'
                    , color: '000000'
                }
                , bottom: {
                    style: 'thick'
                    , color: '000000'
                }
                , left: {
                    style: 'thick'
                    , color: '000000'
                }
            });
            myStyle.Font.Alignment.Horizontal('center');
            var borderstyle = wb.Style();
            borderstyle.Border({
                top: {
                    style: 'thin'
                    , color: '000000'
                }
                , right: {
                    style: 'thin'
                    , color: '000000'
                }
                , bottom: {
                    style: 'thin'
                    , color: '000000'
                }
                , left: {
                    style: 'thin'
                    , color: '000000'
                }
            });
            borderstyle.Font.Alignment.Horizontal('center');
            headercell1.Style(myStyle);
            headercell2.Style(myStyle);


            for (var i = 0; i < usagedata.length; i++) {
                var cell = ws.Cell(2 + i + 1, 2);
                cell.String(usagedata[i].Tool);
                cell.Style(borderstyle);
                var cell1 = ws.Cell(2 + i + 1, 3);
                cell1.Number(usagedata[i].Count);
                cell1.Style(borderstyle);
            }

            UsageData.aggregate([{
                        $match: {
                            $and: [{
                                GroupName: {
                                    $ne: ""
                                }
                            }, {
                                GroupName: {
                                    $ne: "Group1"
                                }
                            }, {
                                GroupName: {
                                    $ne: "Group2"
                                }
                            }]
                        }
                    }
                    , {
                        $group: {
                            _id: '$GroupName'
                            , count: {
                                $sum: 1
                            }
                        }
                    }]
                , function (err, data) {
                    // if there is an error retrieving, send the error. nothing after res.send(err) will execute
                    if (err)
                        res.send(err)

                    var wsg = wb.WorkSheet("Group Usage");
                    wsg.Column(2).Width(35);
                    var headercell = wsg.Cell(2, 2);
                    headercell.String('Group');
                    headercell.Style(myStyle);
                    var headercell = wsg.Cell(2, 3);
                    headercell.String('Usage');
                    headercell.Style(myStyle);


                    for (var i = 0; i < data.length; i++) {
                        var cell = wsg.Cell(2 + i + 1, 2);
                        cell.String(data[i]._id);
                        cell.Style(borderstyle);
                        var cell1 = wsg.Cell(2 + i + 1, 3);
                        cell1.Number(data[i].count);
                        cell1.Style(borderstyle);
                    }


                    wb.write('Report.xlsx', res);
                    var filename = "Report.xlsx";
                    res.setHeader('Content-disposition', 'attachment; filename=' + filename);
                    res.setHeader('Content-type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
                    res.pipe(res);

                });
                    return;


        });


        return;

});

我正在使用Node4Excel从我从Mongodb获取的数据生成excel文件。 API首次返回excel文件。第二次,它不返回任何东西,其他API也没有。请帮忙。

1 个答案:

答案 0 :(得分:1)

我最后使用以下几行来修复它:

 wb.write('Report.xlsx', res,function(){

           var filename = "Report.xlsx";
                res.setHeader('Content-disposition', 'attachment; filename=' + filename);
                res.setHeader('Content-type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
                res.pipe(res);


});