TypeError:React中的this.state.persons.map

问题描述 投票:0回答:1

我正在从python调用REST API,并使用axios在反应中显示它。但是我得到以下错误。我已将状态对象声明为数组,但出现此错误。

TypeError:this.state.persons.map不是函数

http://localhost:8080,我正在获取如下所示的值,在POSTMAN中>>

{
    "name": "test",
    "age": "23",
    "sex": "male"
}
import React from 'react';

import axios from 'axios';

export default class PersonList extends React.Component {
  state = {
    persons: []
  }

  componentDidMount() {
    axios.get(`http://localhost:8080`)    
      .then(res => {
        const persons = res.data;
        //console.log(persons)
        this.setState({ persons });
        console.log(typeof(this.state.persons))
        console.log(this.state.persons)
      })
  }

  render() {
    return (
      <ul>
      { this.state.persons.map(person => <li>{person.name}</li>)}
      </ul>
    )
  }
}

我的python代码是这样的。我正在使用cherrypy

我也在使用@ cherrypy.tools.json_out()返回JSON

@cherrypy.tools.json_out()
    def GET(self):
        name = 'test' 
        age = '23'
        sex = 'male'

        return {'name': name,
                'age': age,
                'sex': sex
                }

我的conf文件

 conf = {
        '/': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
            'tools.sessions.on': True,
            'tools.response_headers.on': True,
            'cors.expose.on': True,
            'tools.response_headers.headers': [('Content-Type', 'application/json')],
        }
    }
```python

我正在从python调用REST API,并使用axios在反应中显示它。但是我得到以下错误。我已将状态对象声明为数组,但出现此错误。 TypeError:this.state ....

reactjs axios cherrypy
1个回答
0
投票

我认为应该是

© www.soinside.com 2019 - 2024. All rights reserved.