简体   繁体   中英

How to fix Cannot read property 'axios-retry' of undefined

I've been facing this issue on axios and axios retry Error

TypeError: Cannot read property 'axios-retry' of undefined
    at getCurrentState (index.js:118)
    at index.js:264
    at tryCallOne (core.js:37)
    at core.js:123
    at JSTimers.js:241
    at _callTimer (JSTimers.js:112)
    at _callImmediatesPass (JSTimers.js:166)
    at MessageQueue.callImmediates [as _immediatesCallback] (JSTimers.js:411)
    at MessageQueue.__callImmediates (MessageQueue.js:388)
    at MessageQueue.js:132

Package.json

      "axios": "1.2.0",
       "axios-retry": "3.3.1",
axiosRetry(axios, {
    retries: 0,
    retryDelay: count => count * 1000,
    },
})
const fetchCall = (url, requestInfo) =>
    axios({
        method: requestInfo.method, 
        url, 
        timeout: requestInfo.timeout,
        headers: requestInfo.headers,
        data: requestInfo.body,
        'axios-retry': {
            retries:1
          },
    })

I've tried to upgrade both packages axios(0.18.1 -> 1.2.0) and axios(3.1.9 -> 3.3.1) retry

I'm not sure if you can add retry dynamically though. Instead try to create a client and use configuration as you did before, something like that:

import axios from 'axios'
import axiosRetry from 'axios-retry'

const axiosClient = axios.create()

axiosRetry(axiosClient, {
    retries: 0,
    retryDelay: count => count * 1000,
  },
})

export default axiosClient

and then just use this client everything and not the one imported from a package directly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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