繁体   English   中英

请求在 axios 上中止。获取 reactJS

[英]Request Aborted on axios.get reactJS

我正在尝试通过来自本地存储的令牌发出获取请求,但它给了我请求中止错误。

这是我的nodeJS代码:

    //Read
app.get('/:username',verify, (req, res) => {
 console.log('Welcome to roffys server')
 Todo.find({'username' : req.params.username})
     .exec((err, todo) => {
      if (err) {
       console.log('Error retrieving todos')
      } else {
       res.json(todo)
      }
     })
})

这是验证 function:

    const jwt = require('jsonwebtoken')

module.exports = function (req,res,next){
    const token = req.header('Authentication')
    if(!token) return res.status(401).send('Access Denied')

    try {
        const verified = jwt.verify(token, 'secretkey')
         req.user = verified
    }catch (err) {
        res.status(400).send(
            'Invalid token'
        )
        next()
    }

这是我在 ReactJS 组件上的 FE:

   componentDidMount() {
    axios
        .get(`http://localhost:8080/${localStorage.getItem('username')}`,{
            headers : {
                Authentication : localStorage.getItem('token')
            }
        })
        .then((res) => {
            this.setState({todos: res.data})
            this.setPageCount()
        })
        .catch((err) => {
            console.log("err", err);
        });
}

你的方法都没有返回任何东西。


componentDidMout () {
  return axios.get(url, config)
            .then (res=> this.setState(myProp: res.data});

......
Back
var verify = require(“./path/to verify”);

    //Read
app.get('/:username',verify, (req, res) => {

return Todo.find({'username' : req.params.username})
     .exec()
      .then(todo=> res.json(todo))
      .catch(console.log);
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM