1

i want to send an api request to some url with that i want to send two query params(start and end date). i used to two input tag for the dates and passed it to url but the "date format exception" is shown

//search page
state= {
    startDate: null,
    endDate: null
    };

    startDatePicker= event=>{
        event.preventDefault();
        var date= event.target.value;
      this.setState({
        startDate: date
      });
    }
     endDatePicker= (event)=>{
      event.preventDefault();
      let date= event.target.value;
      this.setState({
        endDate: date
      });
    }
    submitHandler= (event) =>{
        event.preventDefault();
        this.props.onSearchByDate(this.state.startDate, this.state.endDate);
    }
    render() {
        console.log(this.state.endDate);
        return (
            <div>
                <input type= "date" placeholder= "yyyy-mm-dd"
                 onChange= {this.startDatePicker} name= "startDate"/><br/>
                <input type= "date" 
                    onChange= {this.endDatePicker} name= "endDate"/>
                <button onClick= {this.submitHandler}>
                    Search</button>
            </div>
        );
    }
}
// api page
export const fetchByDate= (startDate, endDate) => dispatch =>{
    console.log(startDate);
    fetch("https://api.nasa.gov/neo/rest/v1/feed?start_date=startDate&end_date=endDate&api_key=[demokey]")
    .then(res => res.json())
    .then(data =>{
        console.log(data);
        return dispatch({
            type: actions.fetchByDates, data: data
        });
    });
}
9
  • Where exactly is the error message thrown? and what is exact error message? Commented Nov 25, 2019 at 17:09
  • this is the error: {code: 400, http_error: "BAD_REQUEST", error_message: "Date Format Exception - Expected format (yyyy-mm-dd) - startDate - Unparseable date: "startDate"", request: "neowsapp.com/rest/v1/…"} Commented Nov 25, 2019 at 17:13
  • you need to use Template Literals(developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…) to replace startDate and endDate in api call Commented Nov 25, 2019 at 17:13
  • use it like this: fetch(`api.nasa.gov/neo/rest/v1/feed?start_date=${startDate}&end_date=${endDate}&api_key=[demokey]`) Commented Nov 25, 2019 at 17:14
  • what's the error now? Commented Nov 25, 2019 at 17:16

2 Answers 2

1

As @mukesh210 mentioned, for the first error, you need to use Template Literals in your fetch string. Please note that Template literals uses back-tick ( ` ) not single quote ( ' )

enter image description here

Change:

fetch("https://api.nasa.gov/neo/rest/v1/feed?start_date=startDate&end_date=endDate&api_key=[demokey]")

To:

fetch(`https://api.nasa.gov/neo/rest/v1/feed?start_date=${startDate}&end_date=${endDate}&api_key=[demokey]`)


For the second error, browsers usually does not support localhost (development environment) to go through the Access-Control-Allow-Origin, to bypass that, you have some options:

1. Alias your localhost to your domain. Depending on your PC, you need to change your hosts file. If you are on Windows, the path is C:\Windows\System32\drivers\etc\hosts. on Mac use your Terminal and type sudo nano /etc/hosts and just add:

127.0.0.1   localhost yourdomain.com


2. Use cors-anywhere your fetch should look like:

fetch(`https://cors-anywhere.herokuapp.com/https://api.nasa.gov/neo/rest/v1/feed?start_date=${startDate}&end_date=${endDate}&api_key=[demokey]`)


3. Use cors-js

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

Comments

1

Change

"https://api.nasa.gov/neo/rest/v1/feed?start_date=startDate&end_date=endDate&api_key=[demokey]"

to

`https://api.nasa.gov/neo/rest/v1/feed?start_date=${startDate}&end_date=${endDate}&api_key=[demokey]`

You aren't passing in the values to the api, just strings. Changing it to the above with tick marks allows you to embed variables into the string directly with ${value} format

Comments

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.