An instance doesn't seem to have CancelToken property
See original GitHub issueAn instance doesn’t seem to have CancelToken property
Lately I needed to time some of the requests made by axios, so following the documentation I created and interceptor for an instance of axios (since I do not want to affect all axios requests, only specific ones):
var axios = require('axios').create(); // Create new instance of axios, to use interceptors safely
function setAxiosMeta(config, meta) {
config.meta = config.meta || {};
config.meta = Object.assign(config.meta, meta);
}
function getAxiosMeta(config) {
config.meta = config.meta || {};
return config.meta;
}
axios.interceptors.request.use(function (config) {
setAxiosMeta(config, {
requestStartTime: Date.now()
});
return config;
}, function (error) {
return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
var meta = getAxiosMeta(response.config);
setAxiosMeta(response.config, {
requestTime: Date.now() - meta.requestStartTime,
requestId: response.headers['x-yt-request-id'],
proxy: response.headers['x-yt-proxy']
});
return response;
}, function (error) {
return Promise.reject(error);
});
We also use request cancellation:
var cancellation = axios.CancelToken.source();
And this line now throws and error:
Uncaught TypeError: Cannot read property 'source' of undefined
at Object.core._createRequestWithCancellation (yt.js:826)
....
Is there a known workaround for creating Cancellation when working with and instance of axios? It isn’t very clear what an instance is, at least after I encountered this behaviour. At first I thought that instance is the same as axios itself, but can be additionally configured.
Context
- axios version: e.g.: v0.17.1
- Environment: e.g.: node v8.9.4, chrome 62, ubuntu Xenial
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:10 (1 by maintainers)
Top Results From Across the Web
How to cancel token using a custom Axios instance?
I would like to use the cancellation feature of Axios but the request fired from custom instance never gets cancelled. It does't get...
Read more >Cancellation | Axios Docs
The axios cancel token API is based on the withdrawn cancelable promises ... CancelToken; const source = CancelToken.source(); axios.get('/user/12345', ...
Read more >Code quality rules overview - .NET - Microsoft Learn
A type or member is marked by using a System.ObsoleteAttribute attribute that does not have its ObsoleteAttribute.Message property specified.
Read more >"Element 'Id' does not match any field or property of class ...
Hello, I have a problem with the Project step in my Facet stage which maps Entity object into DTO: var dataFacet = AggregateFacet....
Read more >VS Code API | Visual Studio Code Extension API
To get an instance of a CancellationToken use a CancellationTokenSource. Properties. isCancellationRequested: boolean.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@dlahuta you did not return config in your request interceptors, this is the reason of
Cannot read property 'cancelToken' of undefined
If you need to have cancelToken in instance (and probably also isCancel) just add them: