0

I am trying to fetch the data from spring boot layer and render it on front end using react js in tabular form . However I see the error mentioned below.

I would like to see the json data in tabular form but I get errors on map function.TypeError: this.state.currencies.map is not a function. Below mentioned is the UI code.

import React,{Component} from 'react';
import ReactDOM from 'react-dom';
import {Table} from 'react-bootstrap';

class AppCrypto extends Component{

  constructor(props) {
  super(props);
  this.state = {currencies: [
    {id:null,currencyname:null,currencyticker:null}
  ]
};
}



componentDidMount() {
   fetch('/secretdata/allcurrency/',{
     method:'GET',
     headers:{
       'Content-Type':'application/json'
     }
   })
   .then( function(response){
     return response.json();
   })
   .then(function(myJson){
      return JSON.stringify(myJson);
   })
   .then (crypto => {
     console.log(crypto);
     this.setState({currencies:crypto});

  });

}

  render(){
    return(
      <div className="container-fluid">
      <Table striped bordered hover variant="dark">
        <thead>
          <tr>
           <th>ID</th>
           <th>CURRENCY_NAME</th>
           <th>CURRENCY_TICKER</th>
          </tr>
        </thead>
        <tbody>
        {this.state.currencies.map(function(item, key){
            return(
              <tr key={key}>
                 <td>{item.id}</td>
                 <td>{item.currencyname}</td>
                 <td>{item.currencyticker}</td>
              </tr>
            )
        })}
        </tbody>
      </Table>
      </div>
    );
  }
}



export default AppCrypto;

1 Answer 1

3

You shouldn't use JSON.stringify. stringify converts the json to a string. You are storing a string in the state instead of an array.

So if you want the data as an array, either use JSON.parse(str) or check if the response already is a valid json.

Sign up to request clarification or add additional context in comments.

2 Comments

good god ,, thank you so much .. I have cleared the json.stringify and it worked out now.
@V3nky if jgoday's answer fixed your problem please mark it as the correct solution, thanks

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.