[英]when sending a request to the server, the node does not receive the req.body
when sending a request to the server, the node does not receive the req.body what could be the mistake?当向服务器发送请求时,节点没有收到 req.body 可能是什么错误?
react:反应:
sendRequest('POST','http://127.0.0.1:5500/api/auth/register', {...form})
function sendRequest(method:string, url:string, bodyObj:any = null){
return fetch(url, {
method: method,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(bodyObj)
}).then(response => response.json())
.then(result => result)
}
node:节点:
router.post('/register',async (req,res)=>{
console.log(req.body)
res.status(200).json(req.body)
})
There are a few things going on here that might cause issues but I think the primary area to investigate is the sendRequest
function is returning a Promise which is asynchronous but then it's also trying to .then
within that same function.这里发生的一些事情可能会导致问题,但我认为要调查的主要领域是
sendRequest
函数正在返回一个异步的 Promise,但它也试图.then
在同一个函数中。
Resolving or rejecting the return promise where you want to handle the data might be a better execution.在要处理数据的地方解析或拒绝返回承诺可能是更好的执行。
// This returns a Promise function sendRequest(method, url, bodyObj) { return fetch(url, { method: method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(bodyObj) }) } sendRequest('POST', 'http://127.0.0.1:5500/api/auth/register', { ...form }).then(result => { console.log(result) }).catch(err => { console.log(err) })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.