Roffy bc
Roffy bc

Reputation: 91

Request Aborted on axios.get reactJS

I'm trying to make get request via token from local storage but it is giving me Request aborted error.

Here is My nodeJS code :

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

Here is the Verify 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()
    }

And here is my FE on ReactJS component:

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

Upvotes: 0

Views: 695

Answers (1)

Ernesto
Ernesto

Reputation: 4272

None of yow methods return anything.


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

Upvotes: 1

Related Questions