Skip to content

Axios.get is not a function error #1

@KJYoung

Description

@KJYoung

I tried to use rescript-axios library with a toy example as below. But, I encountered Uncaught TypeError: Axios.get is not a function. Related Axios version was 1.1.3.

Rescript code

let fetchData = () => { 
    Axios.get("http://localhost:8081/", ()) 
  ->Promise.Js.toResult 
  ->Promise.mapOk(({data}) => Js.log(data))
  ->Promise.tapError(err => {
    switch (err.response) {
      | Some({status: 404}) => Js.log("Not found")
      | e => Js.log2("an error occured", e)
    } 
  })
  ->ignore
}

Compiled Javascript code

import * as Axios from "axios";
...
function fetchData(param) {
  $$Promise.tapError($$Promise.mapOk($$Promise.Js.toResult(Axios.get("http://localhost:8081/", undefined)), (function (param) {
              console.log(param.data);
            })), (function (err) {
          var e = err.response;
          if (e !== undefined && e.status === 404) {
            console.log("Not found");
          } else {
            console.log("an error occured", e);
          }
        }));
}

And, there was two ways to solve this problem.

  • Modify import statement as import Axios, * as _ from "axios";
  • Modify function call as Axios.default.get

Therefore, I adapt the second solution into the rescript-axios code in the PR #2. If there is a better solution to this, Iet me know that. Thank you in advance!

Sincerely, KJYoung.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions