简体   繁体   中英

how to get data from localhost:9200 (elasticsearch) using axios in react

I'm trying to get the data from elasticsearch to map the cards, but after I npm start there was not cards mapped, and I think the problem about NCARMap: resp.data . can you please help me?

//Function to create the cards with thier proipte values
function CreateCards(NCARMap) {
    return(
        <SimpleCard
        key={NCARMap.id}
        theCardId={NCARMap.id}
        cardType={NCARMap.approvetool}
        cardNum={NCARMap.num}
        cardName={NCARMap.name}
        cardDate={NCARMap.date}
        // cardCategory={NCARMap.cardCategory}
        // cardSource={NCARMap.cardSource}
        cardDesc={NCARMap.summary}
        cardURL={NCARMap.image}
        />
    );

}

//create the class
export default class OfficialDocument extends Component {
  constructor(props) {
    super(props);

    this.state = { 
      NCARMap: [],
      NCARMapAS: [],
    };
  }
  componentDidMount(){
    //Get NCARMap data, NCARMapAS used for filtring and sorting the cards
    axios.get('http://localhost:9200/ncar_index/ncar/_search')
        .then(resp => {
            console.log(resp)
            this.setState({
              NCARMap: resp.data, 
              NCARMapAS:resp.data
        })
        console.log(this.state.NCARMap)
    })
}

I don't think NCARMap: resp.data is a problem. console.log was called before axios.get called and it's natural that you see nothing.

At CreateCard , NCARMap is treated as a Object like this NCARMap.id . But you gave default state as a array.

this.state = { 
      NCARMap: [],
      NCARMapAS: [],
    };

I think you have made a mistake with data type. By the way, console.log should be called here.

axios.get('http://localhost:9200/ncar_index/ncar/_search')
        .then(resp => {
            console.log(resp)
            this.setState({
              NCARMap: resp.data, 
              NCARMapAS:resp.data
        }, function() {
        console.log(this.state.NCARMap)
        )
    })

I solve this by change it to:

NCARMap: resp.data.hits.hits,

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