Комментарии:
gold
Ответить🎉🎉
ОтветитьCan we use spread operator to handle object modifying instead of Object.assign
ОтветитьDeleted req is not working
Ответитьhi sir i am facing problem for post and patch req
/without fs.writefilesync it is genrating response in postman
//with fs.writefilesync it is working completitely fine in vscode also records are being added
//into our movies.json file but giving error in postman
I have also pasted my code
const createMovie=(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.writeFileSync('./data/movies.json',JSON.stringify(movies),(err)=>{
res.status(201).json({
status:"sucesss",
//property defined in our custom middleware
data:{
movie:newMovie
}
})
//})
};
//without fs.writefilesync it is genrating response in postman
//with fs.writefilesync it is working completitely fine in vscode also records are being added
//into our movies.json file but giving error in postman
const updatemovie=(req,res)=>{
const id=req.params.id*1;
let movietoupdate=movies.find(el=>el.id===id);
let index= movies.indexOf(movietoupdate);
let output=Object.assign(movietoupdate,req.body);
movies[index]=movietoupdate;
//fs.writeFileSync('./data/movies.json',JSON.stringify(movies),(err)=>{
//200 fetched sucessfully
res.status(200).json({
status:"sucess",
data:{
updatedmovie:output
}
})
//})
}
/*const deleteemovie=(req,res)=>{
const id=req.params.id*1;
let movietodelete=movies.find(el=>el.id===id);
let index= movies.indexOf(movietodelete);
movies.splice(index,1);
fs.writeFileSync('./data/movies.json',JSON.stringify(movies),(err)=>{
//200 fetched sucessfully
res.status(204).json({
status:"sucess",
data:{
movietodeleted:null
}
})
})
}*/
app.get('/api/v1/movies',getAllmovies);
//name is optional here
app.get('/api/v1/movies/:id/:name?',getmovie);
app.post('/api/v1/movies',createMovie);
app.patch('/api/v1/movies/:id',updatemovie);
//app.delete('/api/v1/movies/:id',deleteemovie);