I am getting
"Uncaught (in promise) Error: Request failed with status code 401"
while my other API like GET
, PUT
, and POST
with JWT
is working fine. I am wondering where could I go wrong? Many thanks in advance and greatly appreciate any helps. Thanks again.
//JWT
function JWTAuthenticatToken(req, res, next) {
const token = req.cookies.authcookie
jwt.verify(token, process.env.JWT_TOKEN_SECRET, (err, userData) => {
if (err) return res.sendStatus(401).json({ error: "You have to be logged in!" })
req.user = userData
next()
})
}
//client
const deletePost = async (id) => {
const response = await Axios.delete('http://localhost:5000/api/posts/delpost', { postId: id }, { withCredentials: true })
const deleteData = dbdata.map(item => {
if (item._id == response.data._id) {
return response.data
}
else {
return item
}
})
setDBData(deleteData)
}
//express
router.delete('/delpost', JWTAuthenticatToken, async (request, response) => {
console.log(request.body)
try {
const post = await Post.findOne({ _id: request.body.postId }).populate("postedby", "_id")
if (post.postedby._id === request.user.id) {
post.remove({ _id: request.body.postId })
return response.json({ message: "deleted succesfully" })
}
} catch (error) {
return response.json({ message: error })
}
})