javascript - Iterating over google places JSON response -
hi have been able json response google places , need extract photo reference element it.
i have tried iterate on places results , each places results photo_reference error message.
typeerror: cannot read property '0' or undefined.
what have tried:
response.on('end', function() { var places = json.parse(body); var locations = places.results; locations.foreach(function(loc) { var photoref = loc.photos[0].photo_reference; console.log('photo ref ==' + photoref); }); });
here sample of json response.
{ "html_attributions" : [], "results" : [ { "geometry" : { "location" : { "lat" : 46.7725141, "lng" : 23.62686099999999 }, "viewport" : { "northeast" : { "lat" : 46.77284264999999, "lng" : 23.62798785 }, "southwest" : { "lat" : 46.77152845000001, "lng" : 23.62348045 } } }, "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png", "id" : "18ce6ff7131f1ffd9ce9b2558ba407eed7409aee", "name" : "hotel univers t", "photos" : [ { "height" : 1066, "html_attributions" : [ "\u003ca href=\"https://maps.google.com/maps/contrib/116854836566418330262/photos\"\u003ehotel univers t\u003c/a\u003e" ], "photo_reference" : "coqbdwaaae6zi31lvtxmrm0ortbkdbj3pgenks82rvyp-462yhvrzahhidp2xoweiwsdnpu8ttmkxkhhb2adkhi5gcptxsdx16gtg6rl9gpmogjw9bir1jdiwsgmozie9xguxaqj_s6q9vhv77tqamukc7jtjmbf_bfkqf3pvcmh6ffuuuizehdaf5bmqfqzryedoaa0kbkdghsypd-28mhr9xokrb8dthehjla5bg", "width" : 1069 } ], "place_id" : "chijpzw_f2omsucrnl_mz_sqs2c", "rating" : 4, "reference" : "cmrraaaagfisd-et8v96lnii_v7yz5w80iuyiklpyepkf2dxwxudbm8yofzaldgmv1onfl2ypn6dweidgl4tmafp2weyhpklveu-fxdfjuwa-w5ckapzxv0fdhslsk4hkzpg9yn8ehdut1kzuzs-o2h_eijhgu5_ghsnd6qua9vg54qbt3csnwmag_75lw", "scope" : "google", "types" : [ "gym", "spa", "lodging", "health", "restaurant", "food", "point_of_interest", "establishment" ], "vicinity" : "strada alexandru vaida voevod 53-55, cluj-napoca" }, { "geometry" : { "location" : { "lat" : 46.7720145, "lng" : 23.6257946 }, "viewport" : { "northeast" : { "lat" : 46.77209325, "lng" : 23.62638825 }, "southwest" : { "lat" : 46.77177825, "lng" : 23.62401364999999 } } }, "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png", "id" : "61f80d1a74e00b55a075cacde6fc95aa88bfa83f", "name" : "pizza hut iulius mall", "opening_hours" : { "open_now" : false, "weekday_text" : [] }, "photos" : [ { "height" : 2322, "html_attributions" : [ "\u003ca href=\"https://maps.google.com/maps/contrib/112054611800145890609/photos\"\u003etamara ionescu\u003c/a\u003e" ], "photo_reference" : "coqbdwaaaoday1slwhqwz1sefvhzrqaofcno7dbthsnlmn1ai6lgxmghfjde7dlfdsjxjwsyh_0l3f2mf_-si12umj9ctaysu6sjhrqnaof1wdafndnlbkdbqjvesu0fqjid9v8poku8hhtb2pavre8lkfjf6n1ujiwwoxqlrfhhdjqn_baaehctfmdxtqjfwic3qbozo3sqghtisansbf4axgynt6fnoslovrqtyw", "width" : 4128 } ], "place_id" : "chij-0xlwgomsucr87bdpcmclhi", "price_level" : 1, "rating" : 4.1, "reference" : "cmrraaaaby6poknes7fy7vsbvfxzvofzj5wehpxyd8oufayrgmyowrez-hsxbygocb0srrfd73muhiscp0lk7fvlmk7g2-b1toqxht60odkpkfgbe42qoglnvdnamjo1ngrthgzuehb9tpdl0pw_cscpmcqkerp8ght_kuf3xb1cqllimvvc_bstebrt_w", "scope" : "google", "types" : [ "restaurant", "food", "point_of_interest", "establishment" ], "vicinity" : "strada alexandru vaida voevod 55, cluj-napoca" }, { "geometry" : { "location" : { "lat" : 46.7731014, "lng" : 23.627801 } }, "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png", "id" : "32a93ffe70bbdd2c8485e346837ad61384e67bf1", "name" : "hanul dacilor", "opening_hours" : { "open_now" : false, "weekday_text" : [] }, "photos" : [ { "height" : 2988, "html_attributions" : [ "\u003ca href=\"https://maps.google.com/maps/contrib/102010465058487677046/photos\"\u003eחוסין מוחמד\u003c/a\u003e" ], "photo_reference" : "coqbdwaaalgmxpzeorgpgwzfhex8gq6kwqvl1gs5s4w8n0-_wyfwnq2aaxbuxjizk_exnebi6ucm4yhvyjdobdpvyetj6omj1mg_gtw_bji2mc-75ppbx500rabay_jz-8l9nl4pdc1whf6cwwj08xx2zbhammjvvb-dd_yesjp5qt-gv09pehdhoxdgy57rekjg0n5pz01cghtera3psqe-rkpi5vgjddobho2awq", "width" : 5312 } ], "place_id" : "chijmrm5hmsmsucrlichkiirkmy", "rating" : 4.4, "reference" : "cmrraaaa1mc_ed2g3yhfhzsry8fxge4xida9jq9bxpik6rajpnmfuejn00jcib3priszfocpb3034vayac5_dqy8bp_eqns1hbsc0idqp_hu4dcb_krbwqs6sl7btvsd5rsu9lqmehcdzdomo2yjcnch7n3-t0qbghti9ibzmslx0oquq_oihqkudr_yhg", "scope" : "google", "types" : [ "restaurant", "food", "point_of_interest", "establishment" ], "vicinity" : "iulius mall, strada alexandru vaida voevod 53-55, cluj-napoca" }, { "geometry" : { "location" : { "lat" : 46.771945, "lng" : 23.626012 } }, "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png", "id" : "8d7bb32fd50d968c79c2e5048c12b95cec8cbb7d", "name" : "marty city", "opening_hours" : { "open_now" : false, "weekday_text" : [] }, "photos" : [ { "height" : 1125, "html_attributions" : [ "\u003ca href=\"https://maps.google.com/maps/contrib/114164649734885665448/photos\"\u003emarty city\u003c/a\u003e" ], "photo_reference" : "coqbcwaaaafjwklawx73dgvujkgp2us39gfuilk6crhcfdy-v4d1zjrdzkzhoeqnqu0f5ghy9-mwengsy_2q7cjpi_b9_5oyneyqws4u-zn6l5dxpemdtrnnunwd31b3ba_iey7q_jz8ilmscrqrspsbmq6tffatdnnzuwr0acveocvnwp2mehcw0oerjn23gjoa5knp4gzughq5ztj1rbse1zjbhczaqjvam-fn-q", "width" : 1500 } ], "place_id" : "chiju22cv2omsucr-rhmfykjixa", "rating" : 3.8, "reference" : "cmrraaaahjaksuin_npid0s-cc2ojajxwlzv-soz0j9ow8adirzoqlqe1mmkh3awdwklb9p18ugxzgefxbnkc1_e4s-4gpjstdmoatlxsoxsuqpgl4w0pxvhbzzpnvphcsk5gyheehdaruaahhk1wni7jzexovq3ghs-kvrpren1upj-cw6pjciqidhl8a", "scope" : "google", "types" : [ "restaurant", "food", "point_of_interest", "establishment" ], "vicinity" : "str. alexandru vaida voievod 53, cluj-napoca" },
your code looks correct. maybe should try testing existence of photos
array before accessing it:
locations.foreach(function(loc) { var photoref; if (loc.photos && array.isarray(loc.photos)) { photoref = loc.photos[0].photo_reference; } else { photoref = "no photo in data!"; } console.log('photo ref == ' + photoref); });
Comments
Post a Comment