Node.JS rendering to datatable

Node.JS rendering to datatable

rajgordonrajgordon Posts: 1Questions: 1Answers: 0
edited February 2019 in Free community support

I am trying to render the post json output to html jquery but it will throwing error

"Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client". Please assist me on this

*********************************************************This is my .JS file *************************************************************

mysql = require('mysql');
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
var urlencodedParser = bodyParser.urlencoded({ extended: true });
app.use(express.static(__dirname + '/views'));
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);

var connection = mysql.createConnection({
    host:"",
    user: "",
    password: "",
    database: ""
});
connection.connect();

app.get('/piggy', function (req, res) {
    res.sendFile(__dirname +'/views/index.html');
});

app.post('/doggy',urlencodedParser, function(req,res){
   var username=req.body.ISA;
    var quer1 = "select *  from shipment_events_live where ISA = " + username;
    connection.query(quer1, function(err, result) {
        if (err) 
        {
            res.send("Please enter valid ISA");
        }
    result = JSON.stringify(result);
//   res.send(data);
res.sendFile(__dirname + '/views/output.html');
});
});
app.listen(3330, () => {
    console.log('Server is up and running');
});

*****************************************************This is my HTML file***************************************************************

<html lang="en">

<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js"></script>
    <style>
        body {
            padding-top: 50px;
        }
    </style>
</head>

<body class="bg-info">
    <div class="container">
        <div class="jumbotron"></div>
    </div>
    <div class="container">
        <div class="ZipDatatable">
            <table id="ZipcodesTable" class="table table-bordered table-sm">
                <tr>
                    <th>ISA</th>
                    <th>po</th>
                    <th>asin_units</th>
                    <th>asin</th>
                </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>
</body>
<script>
    $(document).ready(function () {
       $('#ZipcodesTable').DataTable({
            "paging": true,
            "pageLength": 10,
            "processing": true,
            "serverSide": true,
            "ajax": {
                "data":"json",
                "type": "POST",
                "url": "/doggy"
            },
                "columns":
                    [
                        { "data": "ISA" },
                        { "data": "po" },
                        { "data": "asin_units" },
                        { "data": "asin" }
                    ]
           
        });
    });
</script>

</html>

Answers

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    I'm afraid this isn't a Node.JS help forum - you'd be better asking on SO or similar.

    That said, the issue is your sendFile call. As the Express docs note that method will set http headers. But you are calling that method (why, I'm not clear) after you send back the JSON. Hence the error message.

    Allan

  • andrewquarteyandrewquartey Posts: 1Questions: 0Answers: 0
    edited November 2019

    Place the return keyword before all your response methods.

    Like so:

    return res.sendFile(__dirname + '/views/output.html');

    And everywher you are calling a response method. Otherwise the code will try to execute after the response has been sent

This discussion has been closed.