简体   繁体   English

将标头发送到客户端后无法设置标头 node.js 中的错误

[英]Cannot set headers after they are sent to the client Error in node.js

Cannot set headers after they are sent to the client I'm getting this error.在将标头发送到客户端后无法设置标头我收到此错误。 I did some study and I think it's because of more than one callbacks but I'm unable to solve this issue.我做了一些研究,我认为这是因为不止一个回调,但我无法解决这个问题。 Please help me fix this issue.请帮我解决这个问题。 I'm attaching herewith my routes.js code.我附上我的 routes.js 代码。 Any help will be highly appreciable.任何帮助都将是非常可观的。 Thank you.谢谢你。

Here's my routes.js code这是我的 routes.js 代码

const {
   con,g
   sessionStore
 } = require('./config/db');
const { route } = require('./routes/auth');
exports.new = function(req, res){
    message = '';
   if(req.method == "POST"){
      const post  = req.body;
      const username= post.username;
      const title= post.title;
      const state= post.state;
      const category= post.category;
      const description= post.description;
 
      if (!req.files)
                return res.status(400).send('No files were uploaded.');
 
      const file = req.files.uploaded_image;
      var img_name=file.name;
 
         if(file.mimetype == "image/jpeg" ||file.mimetype == "image/png"||file.mimetype == "image/gif" ){
                                 
              file.mv('public/imgs/uploads/'+file.name, function(err) {
                             
               var sql = "INSERT INTO `nt_data`(`username`,`title`,`state`,`category`, `images` ,`description`) VALUES (?,?,?,?,?,?)";
               var query = con.query(sql, [username, title, state, category, img_name, description], function(err) {
                  console.log(err)
                 if (!err) {
                   res.redirect('show/' + username);
                 }
                 else {
                  message = "This format is not allowed , please upload file with '.png','.gif','.jpg'";
                  res.render('new.ejs',{message: message});
                }
               }); 
            }); 
          
   } 
}
   else {
      res.render('new');
   }
 
};

exports.show = function(req, res){
    let message = '';
    var username = req.params.username;
    const sql="SELECT * FROM `nt_data` WHERE `username`='"+username+"'"; 
    con.query(sql, function(err, result){
       console.log(err)
      if(result.length <= 0){
      message = "show not found!";
      
      res.render('show.ejs',{data:result, message: message});
     }else{
      res.redirect('/places');
     }
     res.render('show', {data:result, message: message});
   });
  
};
 
exports.places=function (req,res){
   let message = '';
   var username = req.params.username;
    const sql="SELECT * FROM `nt_data` WHERE `username`='"+username+"'"; 
    con.query(sql, function(err, result){
       console.log(err)
       if(result.length <= 0)
         message = "places not found!";
   res.render('places.ejs',{data:result, message: message});
    });
}

In your show function you execute a database query, inside the callback you have an if else statement with a res.render and a res.redirect.在您的节目 function 中,您执行数据库查询,在回调中您有一个带有 res.render 和 res.redirect 的 if else 语句。 So one way or the other a result is sent.因此,以一种或另一种方式发送结果。 So you cannot send a result when the if statement is completed.因此,当 if 语句完成时,您无法发送结果。 Remove this res.render('show', {data:result, message: message});删除这个res.render('show', {data:result, message: message}); . . line and you are good to go!线,你很高兴去!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 node.js:错误 [ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头 - node.js : Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client 在 Node.JS 中发送到客户端后无法设置标头 - Cannot set headers after they are sent to the client in Node.JS 将标头发送到客户端后无法设置标头 (Node.js) - Cannot set headers after they are sent to the client (Node.js) 使用猫鼬和node.js将标头发送到客户端后无法设置标头 - Cannot set headers after they are sent to the client with mongoose and node.js Node.js 发送到客户端后无法设置标头 - Node.js Cannot set headers after they are sent to the client Node.js Express.js Mongoose 错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头 - Node.js Express.js Mongoose Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client 错误 [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client error in node.js 项目 - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client error in node.js project 在 node.js 中收到错误“将标头发送到客户端后无法设置标头” - Getting the error 'Cannot set headers after they are sent to the client' in node.js 错误 [ERR_HTTP_HEADERS_SENT]:在更新 Node.js 中的配置文件 API 时将标头发送到客户端后无法设置标头 - Error [ERR_HTTP_HEADERS_SENT] : Cannot set headers after they are sent to the client in updating profile API in Node.js 节点JS错误:(将标头发送到客户端后无法设置标头) - Node JS Error: (Cannot set headers after they are sent to the client)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM