简体   繁体   English

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

[英]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.

相关问题 在Docpad中,如何使用节点Webkit代理调试和检查服务器端对象? - In Docpad, how can I debug and examine server-side objects using node webkit agent? 在客户端的 XMLHttpRequest 之后,如何在服务器端获取 FormData 值? - How do I get FormData values in Server-side after XMLHttpRequest in client-side? 如何在Node / Express的客户端中正确呈现服务器端错误? - How to properly render server-side errors in the client for Node/Express? 如何将数据从客户端表单输入传输到服务器端Nodejs脚本? - How do I transfer data from a client-side form input to a server-side Nodejs script? 如何从 MERN 客户端更新 MongoDB 记录 - How do I update MongoDB records from client side in MERN 在服务器端进行反应时,如何使用导入的其他文件类型(例如Webpack)? - When doing react server-side, how do I use imported other file types like Webpack? 如何在服务器端React组件中使用回调值? - How do I use callback value in server-side React component? 在Express.js中,如何仅为登录用户生成服务器端会话? - In Express.js, how do I generate server-side sessions for logged in users only?mo 如何将Google API用作特定的用户服务器端? Node.js - How do I use Google APIs as a specific user server-side? Node.js 使用节点/反应的服务器端渲染。 如何获取数据? - Server-side rendering with Node/React. How do I fetch the data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM