Node.js 我未捕获(承诺中)错误:请求失败,状态代码401

Node.js 我未捕获(承诺中)错误:请求失败,状态代码401,node.js,reactjs,axios,Node.js,Reactjs,Axios,我越来越 “未捕获(承诺中)错误:请求失败,状态代码401” 而我的另一个API,如GET,PUT,以及POST,与JWT配合使用,运行良好。我在想我哪里会出错?非常感谢您的帮助。再次感谢 //JWT function JWTAuthenticatToken(req, res, next) { const token = req.cookies.authcookie jwt.verify(token, process.env.JWT_TOKEN_SECRET, (err, userDa

我越来越

“未捕获(承诺中)错误:请求失败,状态代码401”

而我的另一个API,如
GET
PUT
,以及
POST
,与
JWT
配合使用,运行良好。我在想我哪里会出错?非常感谢您的帮助。再次感谢

//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 })
  }

})

最后,我发现Axios不支持删除。我改为使用post,它成功了。

删除
路由器。删除
。然后就可以了。

您应该在Axios.delete()调用中添加th token头,您的token在哪里?函数JWTAuthenticatToken(req,res,next){const token=req.cookies.authcookie jwt.verify(token,process.env.jwt_token\u SECRET,(err,userData)=>{if(err)return res.sendStatus(401.json){error:“您必须登录!”)req.user=userData next()})JWT令牌对于put post和get工作正常,但不能删除。您可以将其添加到您的问题中吗?这是您的问题中非常重要的一部分,请将其添加到Axios标题中的delete中。delete()如果有帮助,请向上投票。