the middleware express.json() is responsible for parsing incoming request bodies that contain JSON data into js objects. Without this middleware, the body of the request (for example, the data sent via Postman in a POST request) would not be parsed, leaving request.body undefined.
@procademy I have an issue where, after making a POST call, the new data is successfully saved in the JSON file. However, when I make a GET request to retrieve all the movies, only the old records are returned, and the newly added movie doesn't show up. If I restart the server and then make the GET call again, it returns all the movies, including the new one.
Undoubtedly, your content is great! Can you help where can I get resources for further practice and learning thought it seems obvious to someone but there lot of resources in the internet: How to find great resource and keep upskilling ownself: I'm looking forward for your kind Reply 🖤
I am not a professional but I would recommend chatGPT, there's many questions that I would feel embarrassed to ask humans because it feels it should be obvious but with AI I can ask it 99999 silly questions and it would replay with structured, clear replies!
Awesome Explanation
Well explained sir!
Well explained. I'm Grateful🙏
the middleware express.json() is responsible for parsing incoming request bodies that contain JSON data into js objects. Without this middleware, the body of the request (for example, the data sent via Postman in a POST request) would not be parsed, leaving request.body undefined.
req.body is undefined only the id is added whit 201 status m using express "^4.18.2"
great explanation by the way thank u so much!
```JS
app.post('/api/v1/movies', (req, res) => {
console.log(req.body)
const newId = movies[movies.length - 1].id + 1
const newMovie = Object.assign({id: newId}, req.body)
movies.push(newMovie)
fs.writeFile('./Data/movies.json', JSON.stringify(movies), (err) => {
res.status(201).json({
status: "created!",
data: {
movie: newMovie
}
})
})
})
```
@procademy I have an issue where, after making a POST call, the new data is successfully saved in the JSON file. However, when I make a GET request to retrieve all the movies, only the old records are returned, and the newly added movie doesn't show up. If I restart the server and then make the GET call again, it returns all the movies, including the new one.
❤❤❤
Undoubtedly, your content is great!
Can you help where can I get resources for further practice and learning thought it seems obvious to someone but there lot of resources in the internet:
How to find great resource and keep upskilling ownself:
I'm looking forward for your kind
Reply 🖤
I am not a professional but I would recommend chatGPT, there's many questions that I would feel embarrassed to ask humans because it feels it should be obvious but with AI I can ask it 99999 silly questions and it would replay with structured, clear replies!
I'm only getting curly braces {} with nothing in them, despite the countless times I have reviewed my code.
what could be the issue here?
Can you please post your post api code here?
Thank you,,, but I already figured.@@procademy
Why not use spread operator to create a new object ? const newMovie = {id:newId, ...reg.body}
Yes you can do that...that is also an option.