简体   繁体   中英

SyntaxError: Cannot use import statement outside a module — React

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.

At first fix your 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>

    )
}


}}

You can use

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

convert into

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM