Currently I'm trying to call a function with a data response from an ajax call, and I'm getting that my constant stores is not iterable. My AJAX call looks like this
jQuery.ajax({
type: 'GET',
url: "<?php echo JURI::base() . "index.php?option=com_ajax&module=hunter_maps_dev&method=getStoresByZip&zip="?>" zip "&format=json",
datatype : "application/json",
success:function(data){
console.log('success');
const stores = data;
console.log(stores);
buildLocationList(stores);
},
...
function buildLocationList({ features }) {
for (const { properties } of features) {
}
}
and I'm logging it to the console.
I'm getting this for the data in the console: data: "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[\"-104.9981929\",\"39.7164275\"]},\"properties\":{\"store
and this is the mock I was previously using:
const stores = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [-77.034084142948, 38.909671288923]
},
'properties': {
'storeImage' : 'https://via.placeholder.com/150',
'storeName' : 'Test Store',
'phoneFormatted': '(202) 234-7777',
'phone': '2022347336',
'address': '1471 P St NW',
'city': 'Washington DC',
'country': 'United States',
'crossStreet': 'at 15th St NW',
'postalCode': '20005',
'state': 'D.C.'
}
},
]
};
What is missing for me to get this to match the mock format so that it's iterable?
UPDATE: Full response currently
{"success":true,"message":null,"messages":null,"data":"{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[\"-104.9981929\",\"39.7164275\"]},\"properties\":{\"storeImage\":\"1542820219karmalogo2-Copy.png\",\"storeName\":\"Karmaceuticals\",\"phoneFormatted\":\"303-765-2762\",\"address\":\"4 S Santa Fe Drive\",\"city\":\"Denver\",\"country\":\"USA\",\"postalCode\":\"80223\",\"state\":\"Colorado\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[\"-104.8526653\",\"39.7861717\"]},\"properties\":{\"storeImage\":\"1438052652medicineman_logo_web.jpg\",\"storeName\":\"Medicine Man - Denver\",\"phoneFormatted\":\"303-373-0752\",\"address\":\"4750 Nome St.\",\"city\":\"Denver\",\"country\":\"Denver\",\"postalCode\":\"80239\",\"state\":\"Colorado\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[\"-105.0135439\",\"39.7206576\"]},\"properties\":{\"storeImage\":\"1455044497CannaBoticaOrange.png\",\"storeName\":\"CannaBotica \",\"phoneFormatted\":\"303-777-1550\",\"address\":\"219 Vallejo st.\",\"city\":\"Denver\",\"country\":\"United States\",\"postalCode\":\"80223\",\"state\":\"Colorado\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[\"-104.8664385\",\"39.6817321\"]},\"properties\":{\"storeImage\":\"1458987858medicine-man-aurora.jpg\",\"storeName\":\"Medicine Man - Aurora\",\"phoneFormatted\":\"303-923-3825\",\"address\":\"1901 S Havana Street\",\"city\":\"Aurora\",\"country\":\"\",\"postalCode\":\"80014\",\"state\":\"Colorado\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[\"-116.905029\",\"33.0433045\"]},\"properties\":{\"storeImage\":\"1479714765showgrow-logo.jpg\",\"storeName\":\"Showgrow - Ramona\",\"phoneFormatted\":\"760-687-9700\",\"address\":\"736 Montecito Way\",\"city\":\"Ramona\",\"country\":\"United States\",\"postalCode\":\"92065\",\"state\":\"California\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[\"-117.8467455\",\"33.7201192\"]},\"properties\":{\"storeImage\":\"1479715612showgrow-logo.jpg\",\"storeName\":\"Showgrow -Santa Ana\",\"phoneFormatted\":\"949-565-4769\",\"address\":\"1625 E St Gertrude Pl\",\"city\":\"Santa Ana\",\"country\":\"United States\",\"postalCode\":\"92705\",\"state\":\"California\"}}]}"}
CodePudding user response:
You need to parse the JSON string to convert it to an object:
buildLocationList(JSON.parse(stores));