0

Need help please... I have a fetchData function, getting the data from the DB Table Matricula, I just need to capture the records that have the date = Today

How can I only receive data where the date is the same as the current day?

class Matricula extends Component {
    state = {
        datos:[],
        today: new Date()

    }

    componentDidMount = () => {
    this.fetchData()
    }
    fetchData = async () => {
        try {
          const response = await getAll('matricula') 
          console.log("ver: ", response.data); 
          if (response.data.fecha.toLocaleString() === this.state.today.toLocaleDateString()) { // no se que me falta
          this.setState({
            status: "done",
            datos: response.data,
          });

        }
        } catch (error) {
          this.setState({
            status: "error"
          });
        }
      };
    render() {
        const data = this.state.matriculas;
        return (
                <ReactTable 
                    data={data} 
                    contentEditable
                    filterable
                    collapseOnDataChange={false}
          columns={[
                {
                  Header: "Id",
                  accessor: "id"
                },
                {
                  Header: "Name",
                  accessor: "Name"
                },
                {
                  Header: "Date",
                  accessor: "date",
                  id: "date",
                }
              ]
            }
          defaultPageSize={14}
          className="-striped -highlight"
        />
)}
export default Matricula;

the getAll funcion is export function getAll(entity){ return axios({ method: 'get', baseURL: API_URL, headers: headers(), url: entity, }) }

Json
  • 39
  • 8
  • Could you please give the sample response object to give a clear answer ? – simbathesailor Oct 26 '18 at 10:56
  • the getAll function is the getAll funcion is export function getAll(entity){ return axios({ method: 'get', baseURL: API_URL, headers: headers(), url: entity, }) } – Json Oct 26 '18 at 13:18

1 Answers1

0

The optimal way would be to ask for the data that you need, that means asking only for the matriculas of today.

If you can't change this, what you should do is filter them before storing them in the state, something like this:

  this.setState({
    status: "done",
      datos: response.data.filter((matricula)=>{
        return matricula.date === this.state.today //not a proper dates comparison
      }),
    });

Here I'm assuming that your matriculas have an attribute date and I'm comparing it to your this.state.today to filter them out. Keep in mind that you should do a proper date comparison, and that depends on the format you are storing your data, this should help

Alex Serra
  • 141
  • 4