简体   繁体   English

如何修复“错误 [ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头”

[英]how to fix "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client"

*

**> const PORT=8000 **> 常量端口=8000

 const express = require('express') const {v4:uuidv4}=require('uuid') const bcrypt =require('bcrypt') const jwt =require('jsonwebtoken') const cors=require('cors') const {MongoClient}=require('mongodb') const { raw } = require('express') const uri='mongodb+srv://Adi:Adi@cluster0.djy2w.mongodb.net/Cluster0?retryWrites=true&w=majority' const app=express() app.use(cors()) app.use(express.json()) app.get('/',(req,res)=>{ res.json('hello') }) app.get('/user', async (req,res)=>{ const client= new MongoClient(uri) const userId=req.query.userId console.log('userid',userId) try{ await client.connect() const database=client.db('app-data') const users=database.collection('users') const query = {user_id:userId} const user= await users.findOne(query) console.log('usersss',user) res.send(user) }catch(err){ console.log(err) } finally{ await client.close() } }) app.post('/signup',async (req,res)=>{ const client= new MongoClient(uri) const {email,password}=req.body const generateeduderId=uuidv4(); const hashedpassword= await bcrypt.hash(password,10) try{ await client.connect() const database=client.db('app-data') const users= database.collection('users') const existingUser= await users.findOne({email}) if(existingUser){ return res.status(409).send('User already exists... Please Login') } const sanitizedEmail=email.toLowerCase() const data={ user_id:generateeduderId, email:sanitizedEmail, hashed_password:hashedpassword } const inserteduser = await users.insertOne(data) const token=jwt.sign(inserteduser,sanitizedEmail,{ expiresIn:60*24, }) res.status(201).json({token,userId:generateeduderId }) } catch(err){ console.log(err) } }) app.post('/login',async(req,res)=>{ const client= new MongoClient(uri) const {email,password}=req.body try{ await client.connect() const database=client.db('app-data') const users= database.collection('users') const user=await users.findOne({email}) const correctpassword= await bcrypt.compare(password,user.hashed_password) if(user && correctpassword){ const token=jwt.sign(user,email,{ expiresIn:60*24 }) res.status(201).json({token,userId:user.user_id}) } res.status(400).send('Invalid Credentials') } catch(err){ console.log(err) } }) app.get('/users', async(req,res)=>{ const client= new MongoClient(uri) try{ await client.connect() const database=client.db('app-data') const users= database.collection('users') const returnedUsers=await users.find().toArray() res.send(returnedUsers) } finally{ await client.close() } }) app.put('/user', async(req,res)=>{ const client= new MongoClient(uri) const formData=req.body.formData console.log(formData) try{ await client.connect() const database=client.db('app-data') const users=database.collection('users') const query= {user_id:formData.user_id} const updateDocument={ $set:{ first_name:formData.first_name, dob_day:formData.dob_day, dob_month:formData.dob_month, dob_year:formData.dob_year, show_gender:formData.show_gender, gender_identity:formData.gender_identity, gender_interest:formData.gender_interest, url:formData.url, about:formData.about, matches:formData.matches }, } const inserteduser= await users.updateOne(query,updateDocument) res.send(inserteduser) } finally{ await client.close() } }) app.listen(PORT,()=>{ console.log(`server running on port ${PORT}`) }) *** error server running on port 8000 Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at new NodeError (node:internal/errors:371:5) at ServerResponse.setHeader (node:_http_outgoing:576:11) at ServerResponse.header (C:\Users\aadar\Desktop\chat\seayo\server\node_modules\express\lib\response.js:776:10) at ServerResponse.send (C:\Users\aadar\Desktop\chat\seayo\server\node_modules\express\lib\response.js:170:12) at C:\Users\aadar\Desktop\chat\seayo\server\index.js:120:26 { code: 'ERR_HTTP_HEADERS_SENT' }

You are returning the response twice.您返回响应两次。 "res.status" is being called twice in "/users" get api. “res.status”在“/users”中被调用两次得到 api。

As the response is already sent with the headers, it throws an error that you are not allowed to set headers twice.由于响应已经与标头一起发送,它会抛出一个错误,不允许您设置标头两次。

暂无
暂无

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

相关问题 错误:[ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头,获取错误 - Error: [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client, fetch error 错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头 -error - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client -error 这是我收到的错误错误 [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client - this is the error i m getting Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client 错误:错误 [ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头 - error: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client 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 错误 [ERR_HTTP_HEADERS_SENT]:发送到客户端后无法设置标头(POST 方法) - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client (POST method) 错误[ERR_HTTP_HEADERS_SENT]:将标头发送到Node JS中的客户端后,无法设置标头 - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client in Node JS Express 应用程序错误:[ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头 - Express app error: [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client NodeJS SQL 登录错误(ERR_HTTP_HEADERS_SENT]:发送到客户端后无法设置标头) - NodeJS SQL Login Error (ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client) node.js:错误 [ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头 - node.js : Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM