Answer the question
In order to leave comments, you need to log in
Why does undefined come up when sending a PUT request?
Good afternoon! I send a fetch request from the client with this body: body: JSON.stringify({"id": "2"}). I process this put request from the server side, but undefined comes to the request body, what is the problem?
Серверная часть:
router.put('/', async (req, res) => {
const {id} = req.body;
res.send(console.log(id))
});
Клиентская часть:
fetch('http://localhost:5000/api', {
method: 'PUT',
body: JSON.stringify({
"id": "2"
})
})
Answer the question
In order to leave comments, you need to log in
The problem seems to be that the middleware is asynchronous, and there is no notification about the end of its execution (it is not called next()
).
Option 1. Either remove async
it - judging by the synchronous operation of the content, it is not needed here:
router.put('/', (req, res) => {
const {id} = req.body;
console.log(id);
res.send(id);
});
router.put('/', async (req, res, next) => {
const {id} = req.body;
console.log(id);
res.send(id);
next();
});
fetch('http://localhost:5000/api', {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
"id": "2"
})
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question