javascript - Error passing arguements between promises in AngularJS -
i’m building promise chain contains 4 functions. purpose of promise make few api calls. each api call affects following one, , require multiple arguments.
the basic flow of code below this:
geocodelocation
first function, , takes$scope.location
input gets geocodedresult
object containinglat
,lng
, ,formattedaddress
getsearchcount
second function requiresresult
previous function. function queries api using restangular , returnsplucked_result
object.savelocation
function requires bothresult
,plucked_result
create new location, based on whetherplucked_result.search_count
less1
or not.search_count
variable returned function.getphotos
requiresresult
,search_count
arguments. using these builds api request url gets fired off. when response comes back,photos
saved db.
the error occurs within savelocation
function. error being returned is:
typeerror: cannot read property 'search_count' of undefined
here's code (with few comments on what's going on):
var geocodelocation, getphotos, getsearchcount, savelocation; // geocode $scope.location formatted address latitude , longitude geocodelocation = function(location) { return geocoder.geocodeaddress(location).then(function(result) { $scope.geocodingresult = "(lat, lng) " + result.lat + ", " + result.lng + " (address: '" + result.formattedaddress + "')"; console.log(result); // pass result object previous function next function return result; }); }; getsearchcount = function(result) { // fetch locations api (using restangular) return global.locationsroute().getlist().then(function(data) { var plucked_result; // use underscore's _findwhere pluck out location we're looking for, , extract it's search_count plucked_result = _.findwhere(data, { name: result.formattedaddress }); // logging correctly console.log(plucked_result); return plucked_result; }); }; savelocation = function(plucked_result, result) { var newlocation, search_count; // logging incorrectly, , error occurs search_count = plucked_result.search_count; console.log(search_count); // if search_count more 1, increment , update search_count if (!(search_count < 1)) { plucked_result.search_count = search_count + 1; } else { // if search_count less 1, create new location newlocation = { name: result.formattedaddress, lat: result.lat, lng: result.lng, search_count: 1 }; global.locationsroute().post(newlocation); } return search_count; }; // function requires both result , search count build 500px api request string getphotos = function(result, search_count) { var newurl, url; url = global.externalapi() + "search?geo=" + result.lat + "," + result.lng + ",25km&rpp=5&image_size=4&tags=true&sort=votes_count&only=[landscapes][city+%26+architecture][nature][urban+exploration][underwater][travel][journalism][black+and+white]&page=" + search_count + "&consumer_key=" + global.consumerkey(); newurl = restangular.oneurl('500px', url).get(); return newurl.then(function(data) { var newphoto, photo, _i, _len, _ref, _results; $scope.photos = data; _ref = data.photos; _results = []; (_i = 0, _len = _ref.length; _i < _len; _i++) { photo = _ref[_i]; newphoto = { name: photo.name, description: photo.description, image_url: photo.image_url, search_term: result.formattedaddress }; _results.push(global.photosroute().post(newphoto)); } return _results; }); }; $scope.geocode = function() { var location; if ($scope.location) { location = $scope.location; return geocodelocation(location).then(getsearchcount).then(savelocation).then(getphotos); } };
the question here is: how pass result
, plucked_result
, search_count
variables between functions?
solved. promises can't pass more 1 argument between them, need bundle of objects , variables array, , pass array between functions. example:
var getsearchcount, savelocation; getsearchcount = function(result) { return global.locationsroute().getlist().then(function(data) { var passed_data, plucked_result; plucked_result = _.findwhere(data, { name: result.formattedaddress }); passed_data = [result, plucked_result]; return passed_data; }); }; savelocation = function(passed_data) { var newlocation, plucked_result, result, search_count; result = passed_data[0]; plucked_result = passed_data[1]; search_count = plucked_result.search_count; if (!(search_count < 1)) { plucked_result.search_count = search_count + 1; plucked_result.put(); } else { newlocation = { name: result.formattedaddress, lat: result.lat, lng: result.lng, search_count: 1 }; global.locationsroute().post(newlocation); } passed_data = [result, search_count]; return passed_data; };
where passed_data
array containing objects , variables.
Comments
Post a Comment