0

I have created REST API endpoint i.e localhost:5000/api/match/:match_idNow I want to fetch data from this endpoint and display it on frontend but I am getting undefined error.

In server.js :

//Get a particular match stats
app.get('/api/match/:match_id', (req, res) =>{

    let match = req.params.match_id;

    matches.findOne({id: parseInt(match)}).then(Match =>{

        res.json(Match);
    });

});

In matchinfo.js :

import React, { Component } from 'react';

class Matchinfo extends Component {
    constructor(props){
        super(props);
        this.state = {
            info:[],
            loading:true
        };
    }

    componentDidMount(){
        fetch('api/match/:match_id')
        .then(res => res.json())
        .then(res => {
      console.log(res)
      this.setState({
        info:res,
        loading:false
      })
    })
    }

  render() {

    if (this.state.loading) {
            return <img src="https://upload.wikimedia.org/wikipedia/commons/b/b1/Loading_icon.gif" />
    }

    return (
      <div>
            <p class="match">MATCH {info.id}</p>
            <h4>{info.team1}</h4>
            <p>VS</p>
            <h4>{info.team2}</h4>
            <div class="winner">
            <h3>WINNER</h3>
            <h4>{info.winner}</h4>
            </div>
      </div>
    );
  }
}

export default Matchinfo;

In matchinfo component I am getting failed to compile after loader is finished spinning see screenshot for more clarification.

enter image description here

enter image description here

JSON Response :

enter image description here

1 Answer 1

1

Try below updated code. It should work as you expected

import React, { Component } from 'react';

class Matchinfo extends Component {
    constructor(props){
        super(props);
        this.state = {
            info:[],
            loading:true
        };
    }

    componentDidMount(){
        fetch('api/match/:match_id')
        .then(res => res.json())
        .then(res => {
      console.log(res)
      this.setState({
        info:res,
        loading:false
      })
    })
    }

    renderLoading(){
        <img src="https://upload.wikimedia.org/wikipedia/commons/b/b1/Loading_icon.gif" />
    }

  render() {
    const {info} = this.state;
    return (
      <div>
            {this.state.loading ? this.renderLoading(): ''}
            {this.state.info.length > 0 && (
                <div>
                <p class="match">MATCH {info.id}</p>
                <h4>{info.team1}</h4>
                <p>VS</p>
                <h4>{info.team2}</h4>
                <div class="winner">
                    <h3>WINNER</h3>
                    <h4>{info.winner}</h4>
                </div>
                </div>
                )}     
      </div>
    );
  }
}

export default Matchinfo;
Sign up to request clarification or add additional context in comments.

12 Comments

this.state.info.team1 , this.state.info.team2 this.state.info.winner nothing works why ?
Got it. I want to see console.log of this.state.info. Can you do console.log after api response.
Do console.log in render before return like console.log(this.state.info); let me know what it prints
I have created the component Matchinfo which will get rendered onclick So I have to still make onclick functionality.
Man in Content.js you are iterating matches using map but in Matchinfo you are not iterating it because its a single object that you get in response. Thats the difference. This fetch('api/matches') returns list of matches and this fetch('api/match/:match_id') gives only one object that matches the matchid. First understand the functionality.
|

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.