简体   繁体   中英

Node Express Can't set headers after they are sent

I know this has been asked in multiple ways already, but my issue seems to be different from those already posted.

I'm trying to send data from a form into google firebase. I have a node app using express.

Here's my function that is sending the data to firebase:

function validateForm() {

async.series([
    function (callback) {
        var errors = "<strong>The following errors were entered:\n";
        var name = $('#name').val();
        var story = $('#story').val();

        if (name.length < 1) {
            errors += "\n-Please enter a valid name";
            callback("Please enter a valid name", null);
        }

        if (story.length < 1) {
            errors += "\n-Please enter a valid story";
            callback("Please enter a valid story", null);
        }
        console.log("FInished validating");
        callback(null, "finished validating");
    }, function (callback) {
        firebase.database().ref('stories/' + Date.now()).set({
            name: name,
            story: story
        }, function() {
            console.log("Firebase callback");
            callback(null, "sent data!");
        });
    }, function (callback) {
        console.log("Finished!");
        callback(null, "done")
    }
]) 
}

(added some console.logging for clarity to ensure I had the callbacks right)

The submission is trigger by clicking on a div that's styles to look like a button, so I know there's no default behavior of forms issues that are causing the problem. Below is the error I'm getting in Node.

Error: /Users/keegan/WebstormProjects/hack4health/views/error.hbs: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11) at ServerResponse.header (/Users/keegan/WebstormProjects/hack4health/node_modules/express/lib/response.js:719:10) at ServerResponse.send (/Users/keegan/WebstormProjects/hack4health/node_modules/express/lib/response.js:164:12) at res.render.done (/Users/keegan/WebstormProjects/hack4health/node_modules/express/lib/response.js:956:10) at /Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/hbs.js:93:9 at done (/Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/async.js:74:20) at /Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/hbs.js:88:18 at /Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/hbs.js:69:11 at done (/Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/async.js:74:20) at /Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/hbs.js:64:20

Here you are calling the callback up to three times:

if (name.length < 1) {
    …
    callback("Please enter a valid name", null);
}
if (story.length < 1) {
    …
    callback("Please enter a valid story", null);
}
…
callback(null, "finished validating");

That will lead to the callback that writes the response to be called multiple times, failing when it tries to write the headers again after sending the first body.

You will either want to return after calling the callback , or you want to call it only once in the end with the accumulated errors .

事实证明,这是由于从Firebase检索更改时的数据引起的,而不是仅在加载后检索数据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM