question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

An instance doesn't seem to have CancelToken property

See original GitHub issue

An 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:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

27reactions
dingziqicommented, Jul 10, 2018

@dlahuta you did not return config in your request interceptors, this is the reason of Cannot read property 'cancelToken' of undefined

25reactions
adrianbieniascommented, Apr 5, 2018

If you need to have cancelToken in instance (and probably also isCancel) just add them:

const instance = axios.create();
instance.CancelToken = axios.CancelToken;
instance.isCancel = axios.isCancel;
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found