【发布时间】:2020-11-07 03:58:14
【问题描述】:
我想使用我传递给它的id 而不是object ID 从我的数据库中删除一个项目。我该怎么做?
我在deleteOne 上做了一些阅读,但我不太确定如何开始使用它。
每部电影都有一个删除电影的按钮,所以我需要将每部电影的id 传递给删除函数。此处是 MERN 堆栈的新手。
{
"_id": "5fa55741aae528142e96c9e6",
"id": 531219,
"user": "5fa406cf937ce199eeb176a1",
"title": "Roald Dahl's The Witches",
"overview": "large string ... ",
"poster_path": "/betExZlgK0l7CZ9CsCBVcwO1OjL.jpg",
"vote_average": "7.1",
"__v": 0
},
到目前为止我的代码
router.delete('/:id/delete', auth, async (req, res)=>{
const movie = await Movie.findByIdAndRemove(req.body.id)
if(!movie){
res.send('Movie not found')
} else{
movie.remove()
res.send('movie deleted')
}
})
case REMOVE_MOVIE:
return{...state, wishlist:state.wishlist.filter(movie => movie.id !== payload)}
export const removeMovie = (id) => async dispatch => {
try {
await axios.delete(`/wishlist/${id}/delete`)
dispatch({
type: REMOVE_MOVIE,
payload:id
})
} catch (error) {
console.log('unable to delete movie')
console.error(error)
}
}
【问题讨论】:
标签: node.js reactjs mongodb redux