0

For a school project I have to make a site with nodejs.
But I have an error that I don't understand.
Here is the error:

_http_outgoing.js:470
    throw new ERR_HTTP_HEADERS_SENT('set');
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:470:11)
    at ServerResponse.header (/home/kiwi/Epitech/Projects/EpiTodo/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/home/kiwi/Epitech/Projects/EpiTodo/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/home/kiwi/Epitech/Projects/EpiTodo/node_modules/express/lib/response.js:267:15)
    at ServerResponse.send (/home/kiwi/Epitech/Projects/EpiTodo/node_modules/express/lib/response.js:158:21)
    at result.forEach.user (/home/kiwi/Epitech/Projects/EpiTodo/index.js:18:26)
    at Array.forEach (<anonymous>)
    at /home/kiwi/Epitech/Projects/EpiTodo/index.js:17:20
    at Query.db.query [as onResult] (/home/kiwi/Epitech/Projects/EpiTodo/query.js:13:9)
    at process.nextTick (/home/kiwi/Epitech/Projects/EpiTodo/node_modules/mysql2/lib/commands/query.js:72:16)

What I am trying to do is to display all the users of the database with this function:

const db = require("./database");

let displayUser = function (callback) {
    let result = [];
    db.query("SELECT * FROM user", (error, res) => {
        if (error) return callback(error);
        if (res.length) {
            for (let i = 0; i < res.length; i++) {
                result.push(res[i]);
            }
        }
        callback(null, result)
    });
}

module.exports = displayUser;

Then I display it with this, I use a foreach like this:

const express = require("express");
const db = require("./database");
const displayUser = require("./query");
const query = require("./query");
const app = express();
const port = 3000;

app.get("/", (request, response) => {
    response.send("Salut !");
});

app.get("/user", (request, response) => {
    response.type("json");
    result = displayUser(function (error, result) {
        if (error) console.log("ERROR: Database (function displayUser)");
        else {
            result.forEach(user => {
                response.send(user);
            });
        }
    });
});

app.get("*", (request, response) => {
    response.status(404);
    response.send("404");
});


app.listen(port);

Is it possible to use a foreach like that? (or even a for) because I think the error comes from there, because when I do :

response.send(result[0])
response.send(result[1])

It works.

Could someone help me please? Thanks in advance.

I hope I could explain my problem clearly.

Kiwi
  • 9
  • 1
  • 8

2 Answers2

1

Try to replace response.send by response.write in your loop and do response.end() at the end.

More explication in this post

docmurloc
  • 1,201
  • 4
  • 11
0
  1. res.send() or res.json() can only be sent once. This is not Espress.js specific, rather it is how HTTP works.
  2. You don't need to use a forEach to send a list of items as a response, just send the array.
  3. If for some reason you need to manipulate each item in the array, you will need to store the value in a variable, and only then send the response. NO MULTIPLE RESPONSES ARE ALLOWED unless you are using WebSockets or Server-Sent Events.
app.get("/user", (request, response) => {
    response.type("json");
    result = displayUser(function (error, result) {
        if (error) {
          console.log("ERROR: Database (function displayUser)");
        } else {
            response.json(result);
        }
    });
});
Firmino Changani
  • 861
  • 7
  • 11