1

im trying to make a api call with node.js to datasciencetool kit with lat and long from react.js. Whenever user enters address i should get the data from the api call however im just getting 404 error in console. if i replace url with static values like this http://www.datasciencetoolkit.org/coordinates2statistics/37.769456%2c-122.429128?statistics=population_density then it works.

data.js

const fetch = require('node-fetch');

module.exports = (app) => {

  app.post('/search-data', (req, res) => {

    console.log(req);
    let lat = req.body.param.lat;//before: req.query.lat it's wrong

    let long = req.body.param.long;
    console.log(lat);



  const apiUrl = 'http://www.datasciencetoolkit.org/coordinates2statistics' + lat + '%2c' + long+'?statistics=population_density';
        fetch(apiUrl).then(res=>res.json()).then(data=>{

            console.log(data)
               res.send({
                        data
                    });


})
        .catch(err => {
            res.redirect('/error');
        });


       })

}

home.js //react

handleSelect = address => {
    this.setState({
      address,
    });

    console.log(this.state.address);

    geocodeByAddress(address)
      .then(res => getLatLng(res[0]))
      .then(({ lat, lng }) => {
        this.setState({
          latitude: lat,
          longitude: lng,
          isGeocoding: false,
        });

        this.setState({ isLoaded: true });
      })
      .catch(error => {
        this.setState({ isGeocoding: false });
        console.log('error', error); // eslint-disable-line no-console
      });

    console.log(this.state.latitude);
    console.log(this.state.longitude);

     var param = {
      lat: this.state.latitude,
      long: this.state.longitude,
      temp: 1,
    };
    axios
      .post(`http://localhost:5000/search-data`, {
        param,
      })
      .then(data => {
        console.log(data);


    });

  };

latitude and longitude return correct values so im not sure whats going on. Any help is greatly appreciated!

1 Answer 1

1

you forget the / in coordinates2statistics/

 const apiUrl = 'http://www.datasciencetoolkit.org/coordinates2statistics/' + lat + '%2c' + long+'?statistics=population_density
Sign up to request clarification or add additional context in comments.

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.