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));
}
}
})
}
Promise.all(urls.map(url =>}).catch(err => {but you need)).catch(err => {- - Also, instead ofres.send(data)you needreturn data;, then actually finish yourPromise.all()by putting a.then( ... )before.catch( ... )..then(r => r.json())after thefetch()btw.