[英]How do I debug server-side errors on MERN?
I have this front-end code:我有这个前端代码:
export const CreatePage = () => {
const auth = useContext(AuthContext)
const {request} = useHttp()
const [content, setContent] = useState('')
const [title, setTitle] = useState('')
const [lead, setLead] = useState('')
useEffect(() => {
window.M.updateTextFields()
},[])
const postHandler = async () => {
try {
const data = await request('/api/post/generate', 'POST', {title: title, lead: lead, content: content}, {
Authorization: `Bearer ${auth.token}`
})
console.log(data)
} catch (e) {}
}
And this back-end code:而这个后端代码:
router.post('/generate', auth, async (req, res) => {
try {
const baseURL = config.get('baseURL')
const {title, lead, content} = req.body
// if (!title || !lead || !content) {
// return res.status(422).json({error: 'Please, input ALL fields'})
// }
const Post = new Post({
title, lead, content, owner: req.body.user.userId // req.user.userId
})
await Post.save()
res.status(201).json({Post})
} catch (e) {
res.status(500).json({message: 'Something went wrong'})
}})
I've tried a lot of things, but I still get this error.我已经尝试了很多东西,但我仍然得到这个错误。 I know this is a server-side error, but that's all I have been able to figure out.我知道这是一个服务器端错误,但这就是我能够弄清楚的全部。
PS If there are any questions about the code, I will add it later. PS如果对代码有任何疑问,我会在稍后添加。
UPD: By the way, could it be a problem's reason? UPD:顺便问一下,这可能是问题的原因吗? Console log:控制台日志:
[1] Proxy error: Could not proxy request /api/post/generate from localhost:3000 to http://localhost:5000.
Probably, it's because of cors, you just can't send request from different url's.可能是因为cors,你不能从不同的url发送请求。 Try to install cors and configure it:尝试安装cors并配置:
const cors = require("cors");
app.use("/", require('./src/routes'));
app.use(cors({
origin: '*'
}))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.