Saturday, 1 April 2017

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 posed.

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 come 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



via thekeegs

No comments:

Post a Comment