javascript - Extract Yelp's rating -
i have locations.json file store titles, locations (lat, long), , phone numbers. issue i'm facing right might seem trivial others beginner couldn't work way like. want extract ratings yelp's api v3 , add locations.rating array. code have below appends whole response object return yelp, when tried console.log (response.businesses[0].rating) able print out rating. how make return rating? in advance.
var yelpphonesearch = "https://api.yelp.com/v3/businesses/search/phone?phone="; var cors_anywhere_url = 'https://cors-anywhere.herokuapp.com/'; // yelp v3 api doesn't support cors, need use 3rd party proxy service var locations = []; $.getjson('/locations.json', function(data){ (var = 0; < data.length; i++) { var schools = {}; schools.title = data[i].title; schools.location = data[i].location; schools.phone = data[i].phone; schools.rating = $.ajax({ "async": true, "crossdomain": true, "url": cors_anywhere_url + yelpphonesearch + data[i].phone, "method": "get", "headers": { "authorization": "bearer " + yelptoken.access_token, "cache-control": "public, max-age=31536000", } }).done(function(response){ // console.log(response); var rating = response.businesses[0].rating; return rating; }); // push infos locations array locations.push(schools); } });
using jquery promises (which regular promises except guaranteed available in old browsers) can rewrite code so
var yelpphonesearch = "https://api.yelp.com/v3/businesses/search/phone?phone="; var cors_anywhere_url = 'https://cors-anywhere.herokuapp.com/'; // yelp v3 api doesn't support cors, need use 3rd party proxy service $.getjson('/locations.json') .then(function(data){ return $.when.apply($, data.map(function(school) { return $.ajax({ "async": true, "crossdomain": true, "url": cors_anywhere_url + yelpphonesearch + school.phone, "method": "get", "headers": { "authorization": "bearer " + yelptoken.access_token, "cache-control": "public, max-age=31536000", } }).then(function(response) { return { title: school.title, location: school.location, phone: school.phone, rating: response.businesses[0].rating }; }); })); }).then(function() { var locations = [].slice.call(arguments); /* here locations array of { title, location, phone, rating } */ });
note: because of asynchronous nature of $.ajax
can't access array of results outside of last .then
because asynchronous code asynchronous
for completeness - es2015+ native promises, code be
$.getjson('/locations.json').then(data => promise.all(data.map(school => $.ajax({ "async": true, "crossdomain": true, "url": cors_anywhere_url + yelpphonesearch + school.phone, "method": "get", "headers": { "authorization": "bearer " + yelptoken.access_token, "cache-control": "public, max-age=31536000", } }).then(response => ({ title: school.title, location: school.location, phone: school.phone, rating: response.businesses[0].rating })) )) ).then(locations => { /* here locations array of { title, location, phone, rating } */ });
if need access locations
in other parts of code, , assuming code in question not taken function (i.e. vars global scoped)
var yelpphonesearch = "https://api.yelp.com/v3/businesses/search/phone?phone="; var cors_anywhere_url = 'https://cors-anywhere.herokuapp.com/'; // yelp v3 api doesn't support cors, need use 3rd party proxy service var locations = $.getjson('/locations.json') .then(function(data){ return $.when.apply($, data.map(function(school) { return $.ajax({ "async": true, "crossdomain": true, "url": cors_anywhere_url + yelpphonesearch + school.phone, "method": "get", "headers": { "authorization": "bearer " + yelptoken.access_token, "cache-control": "public, max-age=31536000", } }).then(function(response) { return { title: school.title, location: school.location, phone: school.phone, rating: response.businesses[0].rating }; }); })); }).then(function() { return [].slice.call(arguments); });
now, in code ever need use locations, you'll need access result using typical promise methodology
locations.then(function(value) { // in here, value array of locations });
without seeing how using locations
in actual code, may not easy above, because, once deal asynchronous code, need deal properly
Comments
Post a Comment