【问题标题】:Request Aborted on axios.get reactJS在 axios.get reactJS 上请求中止
【发布时间】:2020-06-21 20:21:19
【问题描述】:

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

这是我的 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)
      }
     })
})

这里是验证功能:

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

【问题讨论】:

    标签: javascript node.js reactjs axios


    【解决方案1】:

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

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

    【讨论】:

    • 你能告诉我解决方法吗?
    猜你喜欢
    • 2021-06-29
    • 1970-01-01
    • 2021-05-03
    • 2021-07-22
    • 2022-01-16
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    相关资源
    最近更新 更多