如何在 MERN 上调试服务器端错误?

问题描述

我有这个前端代码

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) {}
}

还有这个后端代码

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,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'})
}})

我尝试了很多方法,但仍然出现此错误。我知道这是服务器端错误,但这就是我所能弄清楚的。

附言如果对代码有任何疑问,我会稍后补充。

UPD:顺便说一句,这可能是问题的原因吗?控制台日志:

[1] Proxy error: Could not proxy request /api/post/generate from localhost:3000 to http://localhost:5000.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)