D
D
DeniSidorenko2020-11-12 10:39:28
React
DeniSidorenko, 2020-11-12 10:39:28

Why request and response goes 2 times with axios?

Good afternoon, I'm making a request through axios and I want to store the data in the redux store.

export const loadAuthDate = async() => {
    let token = localStorage.getItem("auth-token")
    if(token === null) {
      token = ""
    }
    const tokenResponse = await axios.post('/api/user/tokenIsValid', null, {headers: {"x-auth-token": token} })
    if(tokenResponse.data){
      const userRes = await axios.get('/api/user/getUser ', {headers: {"x-auth-token": token, "id": tokenResponse.data.data._id},})
      console.log(userRes)
      return {
        type: LOAD_AUTH_DATE,
        token: token,
        user: userRes.data
      }
    }
}


The code itself on nodejs
const tokenIsValid = async(req,res) => {
  try {
    const token = req.header("x-auth-token");
    if(!token){
      return res.json(false)
    }
    const verified = jwt.verify(token, jwtSecret)
    if(!verified){
      return res.json(false)
    }
    const user = await User.findById(verified.id)
    if(!user){
      return res.json(false)
    }

    return res.status(200).json({data: user,})
  }
  catch (e) {
    return res.status(500).json({error: e.message})
  }
}


The question is why console.log(userRes) displays 2 times for me, and if I look through the network, the calls to the server really go 2 times. This creates some bugs. I thought about useEffect but it's not correct to use in redux
image.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question