标头已经发送了node.js错误

时间:2016-04-06 09:22:12

标签: javascript node.js header

我在nodejs中定义了routes.js文件。除了" UPDATEAUTH"路线。在这种情况下,发生错误:标头已发送。它到底是什么 - > router.post("/updateauth",function(request,response){...});

response.json(found);

时出现错误

我想找到一个解决方案,以便我可以附加一个回调。

var express = require("express");
var app = express();
var login = require("../actions/loginuser");
var register = require("../actions/registeruser");
var initial = require("../actions/initialuser");
var authdata = require("../actions/authdata");
var updateauth = require("../actions/updateauth");
var moment = require("moment");
var now = moment();
var sess;
module.exports = function (app) {
    var router = express.Router();
    var date = new Date();
    accessed_time = now.format('YYYY-MM-DD HH:mm:ss Z');
    router.get("/", function (request, response) {
        console.log("-----\nLog : User Requested Home Page on " + accessed_time + "\n-----");
        if (sess != undefined) {
            var session_Email = sess.Email;
            if (session_Email.length > 0) {
                initial.initialuser(session_Email, function (found) {
                    if (found.status == 0) {
                        response.redirect("/auth");
                    } else {
                        response.redirect("/search");
                    }
                });
                console.log("-----\nLog : User " + session_Email + " has an already Running Session on " + accessed_time + "\n-----");
            }
        } else {
            response.render("index.ejs");
        }
    });
    router.get("/auth", function (request, response) {
        if (sess != undefined) {
            var session_Email = sess.Email;
            if (session_Email.length > 0) {
                response.render("auth.ejs");
            } else {
                request.session.destroy(function (err) {
                    console.log("-----\nLog : User Requested Auth - Redirected to Home because of No Active Session on " + accessed_time + "\n-----");
                    response.redirect("/");
                });
            }
        } else {
            request.session.destroy(function (err) {
                console.log("-----\nLog : User Requested Auth - Redirected to Home because of No Active Session on " + accessed_time + "\n-----");
                response.redirect("/");
            });
        }
        console.log("-----\nLog : User Requested Auth Page on " + accessed_time + "\n-----");
    });
    router.get("/search", function (request, response) {
        console.log("-----\nLog : User Requested Search Page on " + accessed_time + "\n-----");
    });
    router.get("/explore", function (request, response) {
        console.log("-----\nLog : User Requested Explore Page on " + accessed_time + "\n-----");
    });
    router.get("/favourites", function (request, response) {
        console.log("-----\nLog : User Requested Favourites Page on " + accessed_time + "\n-----");
    });
// actions
    router.post("/loginuser", function (request, response) {
        var Email = request.body.Email;
        login.loginuser(Email, function (found) {
            if (found.user_exist == 1) {
                sess = request.session;
                sess.Email = Email;
                request.session.save(function (err) {
                    if (!err) {
                        console.log("-----\nLog : User " + Email + " Session Started on " + accessed_time + "\n-----");
                        console.log(sess);
                    }
                });
            }
            response.json(found);
        });
        console.log("-----\nLog : User " + Email + " Requested LoginUser on " + accessed_time + "\n-----");
    });
    router.post("/registeruser", function (request, response) {
        var Email = request.body.Email;
        var Id = request.body.Id;
        var Firstname = request.body.Firstname;
        var Lastname = request.body.Lastname;
        var Gender = request.body.Gender;
        var Thumbnail = request.body.Thumbnail;
        var Type = request.body.Type;
        if (Email.length > 0 && Id.length > 0) {
            register.registeruser(Email, Id, Firstname, Lastname, Gender, Thumbnail, Type, function (found) {
                if (found.status == 1) {
                    sess = request.session;
                    sess.Email = Email;
                    request.session.save(function (err) {
                        if (!err) {
                            console.log("-----\nLog : User " + Email + " Session Started on " + accessed_time + "\n-----");
                            console.log(sess);
                        }
                    });
                }
                response.json(found);
            });
        } else {
            response.send({"Error": "Registration Failed"});
        }
    });
    router.post("/initialcheck", function (request, response) {
        var Email = request.body.Email;
        initial.initialuser(Email, function (found) {
            response.send(found);
        });
    });
    router.post("/getauth", function (request, response) {
        var session_Email = sess.Email;
        if (session_Email.length > 0) {
            authdata.authdata(session_Email, function (found) {
                response.json(found);
            });
        }
    });
    router.post("/updateauth", function (request, response) {
//take email from session
        if (sess != undefined) {
            var session_Email = sess.Email;
            if (session_Email.length > 0) {
                var Passion1 = request.body.Passion1;
                var Passion2 = request.body.Passion2;
                var Passion3 = request.body.Passion3;
                var Bio = request.body.Bio;
                var Work = request.body.Work;
                var Address = request.body.Address;
                var City = request.body.City;
                var Latitude = request.body.Latitude;
                var Longitude = request.body.Longitude;
                if (Passion1.length > 0 && Bio.length > 0 && Address.length > 0) {
                    updateauth.updateauth(session_Email, Passion1, Passion2, Passion3, Bio, Work, Address, City, Latitude, Longitude, function (found) {
                        console.log(request.headers);
                        console.log("-----\nLog : User " + session_Email + " Updated Initial " + accessed_time + "\n-----");
                        response.json(found);


                    });
                }
            }
        }
    });
    app.use('/', router);
}

发生错误:

 Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
    at ServerResponse.header (G:\spotlight-commit\node_modules\express\lib\respo
nse.js:718:10)
    at ServerResponse.send (G:\spotlight-commit\node_modules\express\lib\respons
e.js:163:12)
    at ServerResponse.json (G:\spotlight-commit\node_modules\express\lib\respons
e.js:249:15)
    at G:\spotlight-commit\routes\router.js:177:10
    at Query.callback (G:\spotlight-commit\node_modules\mongoose\lib\query.js:20
53:9)
    at G:\spotlight-commit\node_modules\mongoose\node_modules\kareem\index.js:17
7:19
    at G:\spotlight-commit\node_modules\mongoose\node_modules\kareem\index.js:10
9:16
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickCallback (node.js:349:13)

1 个答案:

答案 0 :(得分:0)

我认为多次调用函数调用[{ name: "foo", value: "bar" },{name: "foo2", value: "bar2"}].map{ |k| k.values }.to_h

您是否多次看到updateauth.updateauth

在这种情况下,多次调用response.json()会导致错误