简体   繁体   English

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

I have this error and try evething, I read that 'return' can be one cause of the error and promise can be another one but as you can see in the code above none of this is missing in the code.我有这个错误并尝试 evething,我读到“返回”可能是导致错误的原因之一,promise 可能是另一个原因,但正如您在上面的代码中看到的那样,代码中没有任何一个缺失。 In the console.log(docs) it returns the document correctly.在 console.log(docs) 中,它会正确返回文档。 can any one have some solution, tks任何人都可以有一些解决方案,tks

exports.databaseController = (req, res) => {
   const promise = Database.find({}).exec()

   promise
    .then(docs => {
      if (!docs) {
      throw new Error('docs not found')
     }
     console.log(docs)
     return res.status(200).json(docs)
     })
    .catch(err => {
    console.log(err)                      // not found
    return res.status(404).json({ err })  // return your error msg
  })
}

// app
const controllerDatabase = 
require('../controllers/database.controller')

module.exports = app => {
  app.use((req, res, next) => {
  res.header('Access-Control-Allow-Headers', 'x-access-token, 
       Origin, Content-Type, Accept')
  next()
 })

  app.get('/api/v1/database', 
     controllerDatabase.databaseController)
}

// server.js
require('./app/routes/database.routes')(app)

Remove the console.log .删除console.log its will set the header that why you are calling 2nd time you got the error它会设置 header 为什么你第二次调用你得到错误

 console.log(res.json(docs)) // remove

Or if you need to console.log do with direct docs或者,如果您需要 console.log 直接使用docs

 console.log(docs)

You are already sending your response in console.log.您已经在 console.log 中发送您的回复。 Remove the following删除以下

console.log(res.json(docs));

and this error will go away.这个错误将 go 消失。 If you want to console log your docs do this:如果您想控制台记录您的文档,请执行以下操作:

console.log(docs);

暂无
暂无

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

相关问题 使用 express.js 我收到此错误:未捕获的错误 [ERR_HTTP_HEADERS_SENT]: 发送到客户端后无法设置标头 - Using express.js I get this error: Uncaught Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client 错误[ERR_HTTP_HEADERS_SENT]:将标头发送到Node JS中的客户端后,无法设置标头 - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client in Node JS 节点JS:错误[ERR_HTTP_HEADERS_SENT]:发送到客户端后无法设置标头 - Node JS : 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 在 Express JS 中,[ERR_HTTP_HEADERS_SENT]:添加中间件后,无法在将标头发送到客户端后设置标头 - in Express JS , [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client , after adding a middleware 错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头 - Node Express Mongodb - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client - Node Express Mongodb node js 错误 [ERR_HTTP_HEADERS_SENT]: 发送到客户端后无法设置标头并且控制台中没有 javascript 对象 - node js Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client and no javascript object in console next() 不适用于 Express 4,错误 [ERR_HTTP_HEADERS_SENT]:发送到客户端后无法设置标头 - next() is not working on Express 4, Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client Express 应用程序错误:[ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头 - Express app error: [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client express 错误 [ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头 - express Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM