0

Don't know what's the issue here is my code.

exports.submitAlert = function (req, res) {

    const message = dbConnector.escape(req.body.message);
    const from = dbConnector.escape(req.body.from);
    const to = dbConnector.escape(req.body.to);
    const status = dbConnector.escape(req.body.status);
    const actionType = dbConnector.escape(req.body.action_type);

    if (message == null) res.send(JSON.stringify({
        "success": false,
        "data": null,
        "message": "Please write the message"
    }));

    dbConnector.query('INSERT INTO alert (message, from, to, status, action_type) VALUES (' + message + ', ' + from + ', ' + to + ', ' + status + ', ' + actionType + ');',
        function (err, result) {
        console.log(err);
            if (err) res.send(JSON.stringify({"success": false, "data": null}));

            res.send(JSON.stringify({"success": true, "message": "Alert submitted successfully"}));
        });
};

Here is the route

const express = require('express');
const router = express.Router();

const controller = require('../controllers/alertController');



router.post('/submit', controller.submitAlert);



module.exports = router;
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
  • 1
    Possible duplicate of [Error: Can't set headers after they are sent to the client](https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client) – Luca Kiebel Sep 19 '18 at 11:30
  • The problem is, that if there is an error, you are sending `{"success": false, "data": null}` and then `{"success": true, "message": "Alert submitted successfully"}` to the client. You are probably missing an `else` – Luca Kiebel Sep 19 '18 at 11:31

2 Answers2

0

There is a good chance that the res.send get fired more than once considering your control points. You cannot send more than 1 response to a request.

enter image description here

heysulo
  • 182
  • 3
  • 14
0

This problem occurs because if any error comes it'll send the response but after that it'll keeps on executing the code as you're not using the return keyword. It'll tell your compiler to stop. I've updated the code have a look. It should work.

exports.submitAlert = function (req, res) {

    const message = dbConnector.escape(req.body.message);
    const from = dbConnector.escape(req.body.from);
    const to = dbConnector.escape(req.body.to);
    const status = dbConnector.escape(req.body.status);
    const actionType = dbConnector.escape(req.body.action_type);

    if (message == null) return res.send(JSON.stringify({
        "success": false,
        "data": null,
        "message": "Please write the message"
    }));

    dbConnector.query('INSERT INTO alert (message, from, to, status, action_type) VALUES (' + message + ', ' + from + ', ' + to + ', ' + status + ', ' + actionType + ');',
        function (err, result) {
        console.log(err);
            if (err) return res.send(JSON.stringify({"success": false, "data": null}));

            res.send(JSON.stringify({"success": true, "message": "Alert submitted successfully"}));
        });
};

NOTE: another way is to use else but you might get callback hell so it's a good practice to use return instead.

Atishay Jain
  • 1,425
  • 12
  • 22