簡體   English   中英

在AngularJS中的承諾之間傳遞論據時出錯

[英]Error passing arguements between promises in AngularJS

我正在構建一個包含四個功能的承諾鏈。 許諾的目的是進行一些API調用。 每個API調用都會影響以下調用,某些調用需要多個參數。

下面的代碼的基本流程是這樣的:

  1. geocodeLocation是第一個函數,並接受$scope.location輸入,該輸入將被地理編碼到包含latlngformattedAddressresult對象中
  2. getSearchCount是第二個函數,需要上一個函數的result 該函數使用Restangular查詢API,並返回一個plucked_result對象。
  3. saveLocation函數根據plucked_result.search_count是否小於1 ,需要resultplucked_result來創建一個新位置。 此函數返回search_count變量。
  4. getPhotos需要使用resultsearch_count參數。 使用這些工具,可以構建一個可以觸發的API請求URL。 當響應返回時, photos將保存到數據庫。

該錯誤發生在saveLocation函數中。 返回的錯誤是:

TypeError: Cannot read property 'search_count' of undefined

這是代碼(對正在發生的事情有一些評論):

var geocodeLocation, getPhotos, getSearchCount, saveLocation;
// geocode the $scope.location into a formatted address with latitude and 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 the result object from the previous function into the next function
    return result;
  });
};

getSearchCount = function(result) {
  // fetch the locations from the API (using Restangular)
  return Global.locationsRoute().getList().then(function(data) {
    var plucked_result;
    // use underscore's _findWhere to pluck out the location we're looking for, and extract it's search_count
    plucked_result = _.findWhere(data, {
      name: result.formattedAddress
    });
    // this is logging correctly
    console.log(plucked_result);
    return plucked_result;
  });
};

saveLocation = function(plucked_result, result) {
  var newLocation, search_count;
  // this is logging incorrectly, and is where the error occurs
  search_count = plucked_result.search_count;
  console.log(search_count);
  // if the search_count is more than 1, increment and update the search_count
  if (!(search_count < 1)) {
    plucked_result.search_count = search_count + 1;
  } else {
  // if the search_count is less than 1, create a new location
    newLocation = {
      name: result.formattedAddress,
      lat: result.lat,
      lng: result.lng,
      search_count: 1
    };
    Global.locationsRoute().post(newLocation);
  }
  return search_count;
};

// this function requires both the result and search count to build the 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 = [];
    for (_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);
  }
};

這里的問題是:如何在所有函數之間傳遞resultplucked_resultsearch_count變量?

解決了。 承諾之間不能傳遞多個參數,因此您需要將所有對象和變量捆綁到一個數組中,並在函數之間傳遞該數組。 例如:

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;
};

其中passed_data是包含對象和變量的數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM