错误:发送后无法设置标题。

错误:发送后无法设置标题。我讨厌这个问题,因为得到它很多次,尽管我发现我的错误,但我错了。 好的,这是我的代码。错误:发送后无法设置标题。

const mongoose = require ("mongoose"); 

const Spec = require("./specialist");

const Person = require("./person");

const Company = require("./company");

const bcrypt = require("bcryptjs");

module.exports.findUser=function(username,callback){

let query = {email_num:username};

Spec.findOne(query,(err_spec,spec_user)=>{

if(err_spec) throw err_spec;

if(!spec_user){

Person.findOne(query,(err_person,person_user)=>{

if(err_person) throw err_person;

if(!person_user){

Company.findOne(query,(err_company,company_user)=>{

if(err_company) throw err_company;

if(!company_user){

return console.log("Error User Not Found");

}

return callback(null,company_user);

});

}

return callback(null,person_user);

});

}

return callback(null,spec_user);

});

};

module.exports.comparePassword = function(candidatePassword, hash, callback){

bcrypt.compare(candidatePassword, hash, (err, isMatch) => {

if(err) throw err;

callback(null, isMatch);

});

};

module.exports.saveToken = function(username,role,token,callback){

let query = {email_num:username};

let updateToken={updatedToken:token};

if(role==="Person-User"){

Person.findOneAndUpdate(query,updateToken,callback);

}else if(role==="Specialist-User"){

Spec.findOneAndUpdate(query,updateToken,callback);

}else if(role==="Company-User"){

Company.findOneAndUpdate(query,updateToken,callback);

}else{

console.log("Something went goes wrong");

}

}

我已创建3个集合,并且此文件用于处理它们全部。 这是我的主要服务器代码。

const express = require("express"); 

const mongoose = require("mongoose");

const bodyParser = require("body-parser");

const cors = require("cors");

const handlebars = require("express-handlebars");

const app = express();

const passport = require('passport');

const cookieParser = require("cookie-parser");

const config = require("./config/data");

const routes = require("./routes/users");

const company = require("./routes/company");

const person = require("./routes/person");

mongoose.Promise = global.Promise;

let options = {

useMongoClient: true,

reconnectTries: Number.MAX_VALUE,

reconnectInterval: 500,

poolSize: 10,

bufferMaxEntries: 0

};

mongoose.connect(config.database,options);

let db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function() {

console.log(`DB connected ${new Date()}...`);

});

//app.set('views',__dirname+'views');

app.engine('handlebars', handlebars({defaultLayout:false}));

app.set('view engine', 'handlebars');

app.use(bodyParser.json());

app.use(bodyParser.urlencoded({ extended: false }));

app.use(express.static(__dirname + '/public'));

// app.use(cors());

app.use(cookieParser());

// Passport Middleware

// require('./config/passport')(passport);

app.use(passport.initialize());

app.use(passport.session());

app.get("/",(req,res)=>{

res.render("index");

});

// app.get("/forgotPass",(req,res)=>{

// res.render("forgotPass");

// });

app.get("/user", (req,res)=>{

res.render("user");

});

app.get("/login",(req,res)=>{

res.render("login");

});

app.get("/signup",(req,res)=>{

res.render("signup");

});

app.get("/we",(req,res)=>{

res.render("we");

});

app.get("/blog",(req,res)=>{

res.render("blog");

});

app.get("/contactUs",(req,res)=>{

res.render("contactUs");

});

app.get("/userAsApplicant",(req,res)=>{

res.render("userAsApplicant");

});

app.use("/users",routes);

app.use("/company",company);

app.use("/person",person);

app.get("/faq",(req,res)=>{

res.render("faq");

});

app.listen(config.port,()=>{

console.log(`Server running on port ${config.port}....`);

});

对于很多表单处理程序,我对所有请求都使用Ajax。

$(function() { 

$('.subForm').on('submit', function (e) {

$.ajax({

type: 'post',

url: 'http://localhost:3000/users/spec/register',

data: $(this).serialize(),

success:function(data){

if(data.success){

location.href="http://localhost:3000/login"

}else{

location.href="http://localhost:3000/signup"

}

}

});

e.preventDefault();

});

$('.personAuth').on('submit', function (e) {

$.ajax({

type: 'post',

url: 'http://localhost:3000/person/register',

data: $(this).serialize(),

success:function(data){

if(data.success){

location.href="http://localhost:3000/login"

}else{

console.log("Chexav");

location.href="http://localhost:3000/signup";

}

}

});

e.preventDefault();

});

$('.companyAuth').on('submit', function (e) {

$.ajax({

type: 'post',

url: 'http://localhost:3000/company/register',

data: $(this).serialize(),

success:function(data){

if(data.success){

location.href="http://localhost:3000/login"

}else{

location.href="http://localhost:3000/signup"

}

}

});

e.preventDefault();

});

$('.logInForm').on('submit', function (e) {

$.ajax({

type: 'post',

url: 'http://localhost:3000/users/authenticate',

data: $(this).serialize(),

success:function(data){

console.log(data);

if(data.token){

localStorage.setItem("Authorization",data.token);

$.ajax({

type:'get',

url:'http://localhost:3000/users/user',

beforeSend: function(xhr){xhr.setRequestHeader('auth', localStorage.getItem("Authorization"));},

success:location.href="http://localhost:3000/users/user"

})

}

}

});

e.preventDefault();

});

});

而这条路由用于验证。

const express = require("express"); 

const router = express.Router();

const Spec = require("../models/specialist");

const jwt = require("jsonwebtoken");

const config = require("../config/data");

const Model = require("../models/model");

//Registration route

router.post("/spec/register",(req,res)=>{

let date=new Date();

let newUser = new Spec({

name:req.body.spec_name,

email_num:req.body.spec_email,

password:req.body.spec_password,

role:"Specialist-User",

isActive:true,

created:date,

updatedToken:"JWT"

});

if(newUser.password===req.body.spec_confirmPass){

Spec.getUser(newUser.email_num,(error,user)=>{

if(error) throw error;

if(!user){

Spec.addUser(newUser,(err,user)=>{

if(err){

console.log("err");

res.json({success:false,msg:"Somethings Went Wrong"});

} else {

res.header("Content-Type","application/json");

res.json({success:true,msg:"User Registered"});

// res.redirect("/login");

}

});

}else{

res.json({success:false,msg:"User Already Exists"});

}

});

}else{

res.json({success:false,msg:"Password Not Confirmed"});

}

});

//Authentication route

router.post('/authenticate', (req, res,next) => {

const email = req.body.email;

const password = req.body.password;

console.log("UserData");

Model.findUser(email, (err, user) => {

console.log("UserData1");

if(err) throw err;

if(!user){

return res.json({success: false, msg: 'User not found'});

}

Model.comparePassword(password, user.password, (err, isMatch) => {

console.log("UserData2");

if(err) throw err;

if(isMatch){

let payload={

name:user.name,

email:user.email_num,

role:user.role,

deleted:user.deleted,

isActive:user.isActive,

created:user.created,

};

let token = jwt.sign(payload,config.JWT_SECRET,{

expiresIn:1440

});

Model.saveToken(email,user.role,token,(err,success)=>{

if(err) return err;

console.log("Success");

// res.setHeader('Authorization',token);

// res.cookie('Authorization',token);

res.json ({ success: true, token: token });

// res.redirect("https://stackoverflow.com/users/user");

});

} else {

return res.json({success: false, msg: 'Wrong password'});

}

});

});

// res.redirect("/user");

});

router.use(function(req, res, next) {

console.log(req.headers);

let token = req.body.token || req.headers['auth'] || req.query.token || req.cookies.Authorization;

// console.log(token);

if (token) {

jwt.verify(token, config.JWT_SECRET, function(err, decoded) {

if (err) {

console.log(err);

return res.json({ success: false, message: 'Failed to authenticate token.' });

} else {

req.decoded = decoded;

next();

res.render("user");

}

});

} else {

return res.status(403).json({

success: false,

message: 'No token provided.'

});

}

});

router.get("/user", (req,res)=>{

res.render("user");

});

module.exports = router;

作为模板引擎我使用Handlebars.So与登记一切都好,但是当我试图验证服务器带来

Error: Can't set headers after they are sent.

我知道我可以使用cookie,但我想远离这一点。 对不起语言错误,感谢您的帮助。

回答:

Error: Can't set headers after they are sent.

这意味着您要向客户端发送多个响应。

我想你只需要删除您res.render("user");在最后中间件是这样的:

router.use(function(req, res, next) { 

console.log(req.headers);

let token = req.body.token || req.headers['auth'] || req.query.token || req.cookies.Authorization;

// console.log(token);

if (token) {

jwt.verify(token, config.JWT_SECRET, function(err, decoded) {

if (err) {

console.log(err);

return res.json({ success: false, message: 'Failed to authenticate token.' });

} else {

req.decoded = decoded;

// Go next and delete res

next();

// res.render("user");

}

});

} else {

return res.status(403).json({

success: false,

message: 'No token provided.'

});

}

});

希望它能帮助。

以上是 错误:发送后无法设置标题。 的全部内容, 来源链接: utcz.com/qa/266040.html

回到顶部