将它们发送到客户端后无法设置 headers(当我点击 axios post 请求时出错)
Cannot set headers after they are sent to the client (error when i'm striking axios post requrest)
**我正在向我的 Axios 调用发送响应,但我一直收到上述错误**
import User from "../model/UserSchema.js"
export const getuser = async (req, res) => {
res.status(200).json("hello from code with himanhsu 12315464968 darling")
try {
let user = await User.find()
res.json(user)
} catch (error) {
res.json({ message: error.message })
}
}
错误..:
**发送到客户端后无法设置headers
在 ServerResponse.setHeader (_http_outgoing.js:558:11)
在 ServerResponse.header(C:\Users\HIMANSHU\Documents\REACT_MERN_operations\server\node_modules\express\lib\response.js:771:10)
在 ServerResponse.json(C:\Users\HIMANSHU\Documents\REACT_MERN_operations\server\node_modules\express\lib\response.js:264:10)
在 getuser (file:///C:/Users/HIMANSHU/Documents/REACT/16_MERN_operations/server/controller/UserContoller.js:9:13)
在 processTicksAndRejections (internal/process/task_queues.js:93:5)
(使用 node --trace-warnings ...
显示创建警告的位置)
(节点:3732)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这个错误要么是在没有 catch 块的情况下在异步函数内部抛出,要么是因为拒绝了一个没有用 .catch() 处理的承诺。要在未处理的承诺拒绝时终止节点进程,
*我的 API 来自 axios 的调用 *
import axios from "axios"
const url = "http://localhost:8000/users"
export const getUsers = async () => {
return await axios.get(url);
}
当你做的时候
res.status(200).json("hello from code with himanhsu 12315464968 darling")
它写入响应 headers
。您再次调用 res.json()
,它也写入响应 headers,但是 headers
之前已经发送过。
您需要确保调用 res.json
一次。
import User from "../model/UserSchema.js"
export const getuser = async (req, res) => {
// res.status(200).json("hello from code with himanhsu 12315464968 darling")
try {
let user = await User.find()
res.json(user) // status 200 is default here
} catch (error) {
res.json({ message: error.message }).status(400)
}
}
你应该在你的 express 函数中去掉这一行
res.status(200).json("hello from code with himanhsu 12315464968 darling")
你将拥有这个
import User from "../model/UserSchema.js"
export const getuser = async (req, res) => {
try {
let user = await User.find()
res.json(user)
} catch (error) {
res.json({ message: error.message })
}
}
**我正在向我的 Axios 调用发送响应,但我一直收到上述错误**
import User from "../model/UserSchema.js"
export const getuser = async (req, res) => {
res.status(200).json("hello from code with himanhsu 12315464968 darling")
try {
let user = await User.find()
res.json(user)
} catch (error) {
res.json({ message: error.message })
}
}
错误..:
**发送到客户端后无法设置headers
在 ServerResponse.setHeader (_http_outgoing.js:558:11)
在 ServerResponse.header(C:\Users\HIMANSHU\Documents\REACT_MERN_operations\server\node_modules\express\lib\response.js:771:10)
在 ServerResponse.json(C:\Users\HIMANSHU\Documents\REACT_MERN_operations\server\node_modules\express\lib\response.js:264:10)
在 getuser (file:///C:/Users/HIMANSHU/Documents/REACT/16_MERN_operations/server/controller/UserContoller.js:9:13)
在 processTicksAndRejections (internal/process/task_queues.js:93:5)
(使用 node --trace-warnings ...
显示创建警告的位置)
(节点:3732)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这个错误要么是在没有 catch 块的情况下在异步函数内部抛出,要么是因为拒绝了一个没有用 .catch() 处理的承诺。要在未处理的承诺拒绝时终止节点进程,
*我的 API 来自 axios 的调用 *
import axios from "axios"
const url = "http://localhost:8000/users"
export const getUsers = async () => {
return await axios.get(url);
}
当你做的时候
res.status(200).json("hello from code with himanhsu 12315464968 darling")
它写入响应 headers
。您再次调用 res.json()
,它也写入响应 headers,但是 headers
之前已经发送过。
您需要确保调用 res.json
一次。
import User from "../model/UserSchema.js"
export const getuser = async (req, res) => {
// res.status(200).json("hello from code with himanhsu 12315464968 darling")
try {
let user = await User.find()
res.json(user) // status 200 is default here
} catch (error) {
res.json({ message: error.message }).status(400)
}
}
你应该在你的 express 函数中去掉这一行
res.status(200).json("hello from code with himanhsu 12315464968 darling")
你将拥有这个
import User from "../model/UserSchema.js"
export const getuser = async (req, res) => {
try {
let user = await User.find()
res.json(user)
} catch (error) {
res.json({ message: error.message })
}
}