0

I'm new to promises and async programming in javascript, and I'm trying to make multiple API calls to google maps places API in node.js then send data to the client once all the data is received. However, I'm getting some kind of a syntax error saying missing ) after argument list. Sorry if I'm asking a stupid question, I just can't seem to figure the issue out. Thanks for the help!

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

module.exports = (app) => {

    app.post('/search-champ', (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 apiId = 'AIzaSyAeEPop5mofzDJhytOEMtxXaGWFqGB4Q3M';
        const urls = [

            'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=' + lat + ',' + long + '&rankby=distance&type=department_store&key=AIzaSyAeEPop5mofzDJhytOEMtxXaGWFqGB4Q3M',

            'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=' + lat + ',' + long + '&rankby=distance&type=food&key=AIzaSyAeEPop5mofzDJhytOEMtxXaGWFqGB4Q3M'
        ];


        Promise.all(urls.map(url =>


            fetch(url)
            .then(checkStatus)


            .then(data => {

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

                });
            })

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

        function checkStatus(response) {
            if (response.ok) {
                return Promise.resolve(response);
            } else {
                return Promise.reject(new Error(response.statusText));
            }
        }




    })
}
4
  • I think you're missing a curly brace on this line (at the end): Promise.all(urls.map(url => Commented Oct 23, 2018 at 17:05
  • You have }).catch(err => { but you need )).catch(err => { - - Also, instead of res.send(data) you need return data;, then actually finish your Promise.all() by putting a .then( ... ) before .catch( ... ). Commented Oct 23, 2018 at 17:07
  • You also need to insert .then(r => r.json()) after the fetch() btw. Commented Oct 23, 2018 at 17:19
  • @ronald shirman i'll suggest you to make your code as much cleaner as much you can, it will help you in identification of any unwanted syntax error Commented Oct 23, 2018 at 17:26

3 Answers 3

1

Fetch in javascript return an response promise object you have to call the json method on it. Change your checkStatus method to this.

function checkStatus(response) {
            if (response.ok) {
                return Promise.resolve(response.json());
            } else {
                return Promise.reject(new Error(response.statusText));
            }
        }
Sign up to request clarification or add additional context in comments.

3 Comments

1. you have to do this before checking response.ok I assume 2. OP is using node-fetch, an npm package, but it happens to work the same way
@ChrisG Why there is a need to check before response.ok. Instead of returning the promise object (Promise.resolve and Promise.reject ) we can directly return the values function checkStatus(response) { if (response.ok) { return response.json(); } else { return new Error(response.statusText); } }
You're right, .ok is part of the Response returned by fetch. I thought it's a custom field by the OP that's only available in the actual response data.
0

The res.send({ data }); is called for every url in the array, this is probably not what you want, to wait for all api calls to complete, place the res.send() in the then() of the Promise.all()

Promise.all(urls.map(url =>
  fetch(url)
  .then(checkStatus)
).then(data => {
  console.log(data)
  res.send({ data });
}).catch(...)

Comments

0

Working solution:

const fetch = require('node-fetch');
module.exports = (app) => {
    app.post('/search-champ', (req, res) => {
        let lat = req.body.param.lat; //before: req.query.lat it's wrong
        let long = req.body.param.long;
        const apiId = 'AIzaSyAeEPop5mofzDJhytOEMtxXaGWFqGB4Q3M';
        const urls = [
            'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=' + lat + ',' + long + '&rankby=distance&type=department_store&key=AIzaSyAeEPop5mofzDJhytOEMtxXaGWFqGB4Q3M',
            'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=' + lat + ',' + long + '&rankby=distance&type=food&key=AIzaSyAeEPop5mofzDJhytOEMtxXaGWFqGB4Q3M'
        ];
        var apiData = urls.map( (url) => {
            return fetch(url).then(checkStatus);
        });
        Promise.all(apiData)
            .then( (data) => {
            res.send({
                data: data
            });
        });

        function checkStatus(response) {
            if (response.ok) {
                return Promise.resolve(response);
            } else {
                return Promise.reject(new Error(response.statusText));
            }
        }
    });
}

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.