I'm building an API endpoint which will return details and a single photo of a Google place using express.js and Node.js client library for Google Maps API Web Services. The endpoint below returns details of a place and I'm having trouble retrieving the URL of a photo. Below is the code in question.
There are two requests to the google maps client:
- Get place details based on
req.params.id
and the details contain aphotos
array Get photo using the
photo_reference
from abovevar googleMapsClient = require('@google/maps').createClient({ key: 'mykeyhere', Promise: Promise }); exports.getGglPlace = function(req, res) { googleMapsClient.place({ placeid: req.params.id }).asPromise() .then((response) => { var venue = response.json.result if (venue.photos[0]) { googleMapsClient.placesPhoto({ photoreference: venue.photos[0].photo_reference, maxwidth: 200 }).asPromise() .then((photo) => { console.log("Photo:", photo); // this returns a massive chunk of data which I think contains the actual image object also venue.photoURL = photo.url; // this doesn't work res.send(venue); }) .catch((err)=>{ console.log("Error Getting Photo!", err); res.send(venue); }) } else { res.send(venue); } }) .catch((err) => { res.send(404); }) }
Any idea how to obtain the URL from the response which is called photo
in the code above?
If I try going to the API directly through the browser or Postman, the URL gets redirected to the actual source URL of the image, which is what I want to add to my venue
object.
Redirects to (this is what I want photo.url
to return): https://lh3.googleusercontent.com/p/AF1QipPVD12HA5FBnjmiqVLphYgjfPtIPydn4Ie-xGKr=s1600-w200
Any help is appreciated.
P.S. My first post here - sorry if I'm not clear enough with my question.