简体   繁体   English

我正在尝试从Rest API中获取json数据以进行反应,但是它显示了一些错误

[英]I am trying to fetch json data from Rest API into react, but it showing some error

I am trying to fetch the data from Rest API "TVMaze". 我正在尝试从Rest API“ TVMaze”获取数据。 the data is in json format. 数据为json格式。 I am using react.js for this 我为此使用react.js

this is my myclass.js file 这是我的myclass.js文件

    import React from "react";
    import ReactDOM from "react-dom";
    import axios from "axios";

    function Myclass(){
      return(
        axios.get("http://api.tvmaze.com/schedule?country=US&date=2019-05-01")
        .then (function(response){
          console.log(response);
        });
      );
    }  

    export default Myclass;

This is my index.js file: 这是我的index.js文件:

 import React from "react";
    import ReactDOM from "react-dom";
    import Myclass from "./myclass.js";

    import "./styles.css";

    function App() {
      return (
        <Myclass/>
      );
    }

    const rootElement = document.getElementById("root");
    ReactDOM.render(<App/>, rootElement);

This is the error message that I am getting: 这是我收到的错误消息:

    SyntaxError: /src/myclass.js: Unexpected token, expected "," (10:6)

    src/myclass.js: Unexpected token, expected "," (10:6) 8 | .then (function(response){ 9 | console.log(response); > 10 | }); | ^ 11 | ); 12 | } 13 | 8 | .then (function(response){ 9 | console.log(response);
> 10 | }); | ^ 11 | ); 12 | } 13 |
    browser
    Parsing error: Unexpected token, expected "," 8 | .then (function(response){ 9 | console.log(response); > 10 | }); | ^ 11 | ); 12 | } 13 | (null)
    eslint

There are several issues in your code. 您的代码中有几个问题。 A react component must either return something to display (eg: dom elements) or null but here you return a Promise when you write react组件必须返回要显示的内容(例如dom元素)或为null但是在此处您编写时会返回Promise

  return(
    axios.get("http://api.tvmaze.com/schedule?country=US&date=2019-05-01")
    .then (function(response){ 
      console.log(response);
    });
  );

Plus you have a space between then and function which raises the error. 另外,您在thenfunction之间还有一个空格,这会引发错误。

When you want to show data coming from an API request, it's good practice to call your API from within componentDidMount and store your data in the state of your component. 当您想显示来自API请求的数据时,最好在componentDidMount内调用API并将数据存储在componentDidMountstate中。

You can do it with class-based component: 您可以使用基于类的组件来做到这一点:

import React from "react";
import axios from "axios";

class Myclass extends React.Component{

  state = {data: null};

  componentDidMount(){
    axios.get("http://api.tvmaze.com/schedule?country=US&date=2019-05-01")
      .then(function(response){
        this.setState({data: response})
      });
    }

  render(){
    if(!this.state.data){
      return null; //or a loading spinner or whatever 
    }
   return data // here depending on the format of the data (array or object) you might need to do some process.
  }
}  

export default Myclass;

Or with hooks: 或带钩子:

import React, {useEffect} from "react";
import axios from "axios";


const MyClass = function(){
  const [data, setData] = useState(null);

  useEffect(() => {
    axios.get("http://api.tvmaze.com/schedule?country=US&date=2019-05-01")
    .then(function(response){
     setDate(response)
    });
  }, []) // with an empty array as a dependency, the API call will be run only once when the component is mounted.

  if(!data){
    return null;
  }

  return data // same comment than before to show your data properly
}

Hope it helps. 希望能帮助到你。

Your myclass component should look as follows. 您的myclass组件应如下所示。

import React ,{useEffect} from "react";
import ReactDOM from "react-dom";
import axios from "axios";

function Myclass(){
 // The below useEffect is equivalent to a componentDidMount.
  useEffect(()=>{
     axios.get("http://api.tvmaze.com/schedule?country=US&date=2019-05-01")
       .then (function(response){
         console.log(response);
       });
  },[]);

  return(

  );
}  

export default Myclass;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试从 mongodb 连接我的 server.js express api 但它显示一些错误我不明白 - I am trying to connect my server.js express api from mongodb but it showing some error i don't understand it 我可以成功地从 API 中获取数据。 我可以读取一些属性,但尝试读取错误中的大部分结果 - I can fetch the data from an API in react succesfuly. I can read some of the properties but trying to read most of the results in erros 从Java ES6中的Prestashop的Rest API中获取数据(json)(React Native) - Fetch data (json) from Prestashop's Rest API in Javascript ES6 (React Native) 我正在尝试从用户输入中获取一些数据到 JSON 文件中。我收到此消息 - I am trying to get some data from user input into a JSON file.I get this message 尝试从反应应用程序的本地主机 web api 端点获取数据 - NoCors 错误 - Trying to Fetch data from a localhost web api end point from react app - NoCors error 我正在尝试使用fetchApi来获取数据,但出现此错误 - Am trying to use fetchApi to fetch data but i get this error 当我尝试通过在反应中使用 useEffect 从 API 获取数据时遇到问题 - I'm fetching a problem when I'm trying to fetch data from an API by using useEffect in react 我正在尝试从 phpmyadmin 数据库中获取信息,但在浏览器中显示无法获取/员工,并且在命令提示符下没有错误 - I am trying to fetch information from phpmyadmin database, but in browser it's showing cannot get/employee and in command prompt there is no error 我正在尝试从API提取JSON文件 - I'm trying to fetch a JSON file from an API 我无法从此 API 获取数据。 这是代码 - I am unable to fetch data from this API. Here is the code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM