mkr231
mkr231

Reputation: 100

REST API call works only once

I have written a Nodejs REST API application with a number of functions. However when I consume one of the APIs it returns me the excel file it is supposed to return and everything is fine until there. After that the application stops responding and all the other functions stop working. The cde for the problematic function is following:

    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;

});

I am using Node4Excel to generate an excel file from the data I get from my Mongodb. The API returns the excel file for the first time. Second time onwards, it doesnt return anything and neither does the other APIs. Please help.

Upvotes: 0

Views: 780

Answers (1)

mkr231
mkr231

Reputation: 100

I finally fixed it by using the following lines:

 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);


});

Upvotes: 1

Related Questions