Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在AngularJS中的承诺之间传递参数时出错_Javascript_Angularjs_Function_Promise_Angular Promise - Fatal编程技术网

Javascript 在AngularJS中的承诺之间传递参数时出错

Javascript 在AngularJS中的承诺之间传递参数时出错,javascript,angularjs,function,promise,angular-promise,Javascript,Angularjs,Function,Promise,Angular Promise,我正在构建一个包含四个功能的承诺链。承诺的目的是进行一些API调用。每个API调用都会影响下一个API调用,有些API调用需要多个参数 以下代码的基本流程如下: geocodeLocation是第一个函数,它采用$scope.location输入,将地理编码到包含lat、lng和formattedAddress getSearchCount是第二个需要上一个函数的结果的函数。此函数使用restanglar查询API,并返回一个pucked_结果对象 saveLocation功能需要result和

我正在构建一个包含四个功能的承诺链。承诺的目的是进行一些API调用。每个API调用都会影响下一个API调用,有些API调用需要多个参数

以下代码的基本流程如下:

  • geocodeLocation
    是第一个函数,它采用
    $scope.location
    输入,将地理编码到包含
    lat
    lng
    formattedAddress
  • getSearchCount
    是第二个需要上一个函数的
    结果的函数。此函数使用restanglar查询API,并返回一个
    pucked_结果
    对象
  • saveLocation
    功能需要
    result
    pucked\u result
    根据
    pucked\u result是否小于
    1
    来创建新位置。此函数返回
    search\u count
    变量
  • getPhotos
    需要
    result
    search\u count
    参数。使用这些,它构建了一个API请求URL,该URL会被触发。当响应返回时,
    照片
    将保存到数据库中
  • 错误发生在
    saveLocation
    函数中。返回的错误为:

    TypeError: Cannot read property 'search_count' of undefined
    
    下面是代码(对正在发生的事情有一些注释):

    var geocodeLocation、getPhotos、getSearchCount、saveLocation;
    //将$scope.location地理编码为带纬度和经度的格式化地址
    地理编码定位=功能(位置){
    返回Geocoder.geocodeAddress(位置)。然后返回(函数(结果){
    $scope.geocodingResult=“(lat,lng)”+result.lat+,“+result.lng+”(地址:“+result.formattedAddress+”)”;
    控制台日志(结果);
    //将结果对象从上一个函数传递到下一个函数
    返回结果;
    });
    };
    getSearchCount=函数(结果){
    //从API获取位置(使用Restanglar)
    返回Global.locationsRoute().getList().then(函数(数据){
    var提取结果;
    //使用下划线的_findWhere提取我们要查找的位置,并提取其搜索计数
    Pucked_result=u.findWhere(数据{
    名称:result.formattedAddress
    });
    //这是正确的日志记录
    控制台日志(采集的结果);
    返回采集的结果;
    });
    };
    saveLocation=函数(采集的结果,结果){
    var新位置、搜索和计数;
    //这是错误的日志记录,并且是发生错误的地方
    search\u count=采集的结果。search\u count;
    控制台日志(搜索计数);
    //如果搜索计数大于1,则递增并更新搜索计数
    如果(!(搜索计数<1)){
    采集的结果。搜索计数=搜索计数+1;
    }否则{
    //如果搜索计数小于1,请创建一个新位置
    新位置={
    名称:result.formattedAddress,
    lat:result.lat,
    lng:result.lng,
    搜索计数:1
    };
    Global.locationsRoute().post(newLocation);
    }
    返回搜索计数;
    };
    //此函数需要结果和搜索计数来构建500px API请求字符串
    getPhotos=函数(结果、搜索计数){
    var newUrl,url;
    url=Global.externalAPI()+“search?geo=“+result.lat+”,“+result.lng+”,25km&rpp=5&image\u size=4&tags=true&sort=voces\u count&only=[scapes][City+%26+Architecture][Nature][Urban Urban Exploration][Underwater][Journalism Black+and+White]&page=“+search\u count consumer key=“+Global.consumerKey=”;
    newUrl=Restangular.oneUrl('500px',url.get();
    返回newUrl.then(函数(数据){
    变量newPhoto,photo,_i,_len,_ref,_results;
    $scope.photos=数据;
    _ref=数据。照片;
    _结果=[];
    对于(_i=0,_len=_ref.length;_i<_len;_i++){
    照片=_ref[_i];
    新照片={
    姓名:photo.name,
    description:photo.description,
    image\u url:photo.image\u url,
    搜索词:result.formattedAddress
    };
    _结果.push(Global.photoroute().post(newPhoto));
    }
    返回结果;
    });
    };
    $scope.geocode=函数(){
    var定位;
    if($scope.location){
    位置=$scope.location;
    返回geocodeLocation(location)。然后返回(getSearchCount)。然后返回(saveLocation)。然后返回(getPhotos);
    }
    };
    

    这里的问题是:如何在所有函数之间传递
    结果
    采集的结果
    搜索计数
    变量。承诺之间不能传递多个参数,因此需要将所有对象和变量绑定到一个数组中,并在函数之间传递该数组。例如:

    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;
    };
    
    var getSearchCount,saveLocation;
    getSearchCount=函数(结果){
    返回Global.locationsRoute().getList().then(函数(数据){
    var传递了_数据,采集了_结果;
    Pucked_result=u.findWhere(数据{
    名称:result.formattedAddress
    });
    传递的_数据=[结果,采集的_结果];
    返回传递的_数据;
    });
    };
    saveLocation=函数(传递的\u数据){
    var newLocation,pucked_result,result,search_count;
    结果=通过的_数据[0];
    采集的_结果=通过的_数据[1];
    search\u count=采集的结果。search\u count;
    如果(!(搜索计数<1)){
    采集的结果。搜索计数=搜索计数+1;
    拔出结果。放置();
    }否则{
    新位置={
    名称:result.formattedAddress,
    lat:result.lat,
    lng:result.lng,
    搜索计数:1
    };
    Global.locationsRoute().post(newLocation);
    }
    传递的_数据=[结果,搜索_计数];
    返回传递的_数据;
    };
    

    其中
    passed\u data
    是一个包含对象和变量的数组。

    尝试从
    saveLocation
    I get
    ReferenceError中删除第二个参数:删除两个参数中的任何一个时,未定义Pucked\u结果。
    
    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;
    };