Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with axios instance and getUri #2174

Closed
Midnighter opened this issue May 25, 2019 · 6 comments
Closed

Problem with axios instance and getUri #2174

Midnighter opened this issue May 25, 2019 · 6 comments

Comments

@Midnighter
Copy link

Describe the bug

I would like to use the new getUri method on an axios instance. However, when I attempt this I get a type error.

TypeError: Cannot read property 'indexOf' of undefined
buildURL
node_modules/axios/lib/helpers/buildURL.js:56
  53 | }
  54 | 
  55 | if (serializedParams) {
> 56 |   url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams;
     | ^  57 | }
  58 | 
  59 | return url;

To Reproduce

The following works:

import axios from "axios";

export const getFulltextURL = reference => {
  return axios.getUri({
    url: "http://localhost",
    params: {
      "url_ver": "Z39.88-2004",
      "url_ctx_fmt": "info:ofi/fmt:kev:mtx:ctx",
      "ctx_ver": "Z39.88-2004",
      "ctx_enc": "info:ofi/enc:UTF-8",
      "rft_genre": "article",
      "rft_val_fmt": "info:ofi/fmt:kev:mtx:journal",
      "rft.doi": reference.doi[0],
      "rft.issn": reference.issn[0]
    }
  });
};

but with an axios instance it does not:

import axios from "axios";

const fulltextResolver = axios.create({
  baseURL: "http://localhost",
  params: {
    "url_ver": "Z39.88-2004",
    "url_ctx_fmt": "info:ofi/fmt:kev:mtx:ctx",
    "ctx_ver": "Z39.88-2004",
    "ctx_enc": "info:ofi/enc:UTF-8",
    "rft_genre": "article",
    "rft_val_fmt": "info:ofi/fmt:kev:mtx:journal",
  }
});

export const getFulltextURL = reference => {
  return fulltextResolver.getUri({
    params: {
      "rft.doi": reference.doi[0],
      "rft.issn": reference.issn[0]
    }
  });
};

I have also tried creating the instance with url and baseUrl instead but then I get a different type error.

Expected behavior

I expect the second example to return the same URL with the parameters of the instance and the method call being merged.

Environment

  • Axios Version 0.19.0-beta.1
  • OS: Ubuntu 18.04
  • Browser Chromium, Firefox
  • Browser Version 73.0.3683.86, Firefox 67.0 (on Firefox buildUrl is undefined)
  • React 16.8.6
@Midnighter Midnighter added the bug label May 25, 2019
@jasonsaayman
Copy link
Member

@Midnighter this seems to relate to #2190, please let me know if that issue is addressing the same issue, there is a pull request for that issue.

@Alanscut
Copy link
Collaborator

@Midnighter url is a required field, you can verify if based on #2196 and #2555 can meet your needs?

TypeError: Cannot read property 'indexOf' of undefined

@Midnighter
Copy link
Author

I will give it a try over the weekend.

@pvsiddhartha
Copy link

Following.
I encountered the similar issue.

buildURL.js:62 Uncaught (in promise) TypeError: Cannot read property 'indexOf' of undefined
    at buildURL (buildURL.js:62)
    at dispatchXhrRequest (xhr.js:30)
    at new Promise (<anonymous>)
    at xhrAdapter (xhr.js:12)
    at dispatchRequest (dispatchRequest.js:52)

@jedmonsanto
Copy link

jedmonsanto commented Apr 14, 2020

@pvsiddhartha i encountered the same problem and to only find out that my url is invalid.

@Midnighter
Copy link
Author

@Alanscut I have made a code sandbox and noticed a few things:

  1. url is a required key in the config. I can work around that by passing an empty string.
  2. As noted in Fix params merging #2196 the parameters do not yet get merged (version 0.19.2).
  3. As described in Update 'geturi' method for more information #2555, I do not get the full URI.
  4. The originally reported error has disappeared 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants