0

I am trying to pull information from this api for hospitals. I was able to successfully pull from another api using pretty much the same code. Can't figure out a solution, keep getting "SyntaxError: Cannot use import statement outside a module." This is the code that I am working with:

The components folder has this file only.

    import React from "react";
import getHospitals from "../other/api";

function loadHospitals() {

    useEffect(() => {
        loadHospitals()
    }, []);

    HospitalsAPI.getNews()
        .then(res => {
            console.log(res)
        })
        .catch(err => console.log(err));
};

export default loadHospitals;

A separate file has this file:

import axios from "axios";

export default {
    getHospitals: function() {
        return axios.get("https://services1.arcgis.com/Hp6G80Pky0om7QvQ/arcgis/rest/services/Urgent_Care_Facilities/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json")
    },
};

Any help is appreciated.

1
  • Are you using create react app? Is ALL this code inside src folder? If it’s not, try putting all into src. Commented Apr 19, 2020 at 4:49

2 Answers 2

1

At first fix your Cross-Origin issue You should also fix Cross-Origin  issue Try to use like this

import React from "react";

import axios from "axios";


export default class loadHospitals extends React.Component {


componentDidMount() {
    axios
    .get("https://services1.arcgis.com/Hp6G80Pky0om7QvQ/arcgis/rest/services/Urgent_Care_Facilities/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json")
    .then(res => {
        console.log(res);

    })
    .catch(err => {

    });

}

render() {
    return (

        <h2>
            test hospital
        </h2>

    )
}


}}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! So changing it from a function to a React.Component was what I was doing wrong?
0

You can use

import getHospitals from "../other/api";

convert into

import { getHospitals } from "../other/api";

1 Comment

The error is pointing at the import React from "react"; statement, and I did try what you said too by the way. Gave me the same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.