简体   繁体   中英

What is the correct way/can you chain two AngularJs service calls and then perform a function with the returned data?

I would like to chain these two service calls, and with the results perform a forEach loop to filter my data, but get a TypeError: "SocialMediaUserService.channelProfiles is not a function" message in Chrome.

However, this works in IE, and no warning or message. :)

function getChannelProfiles() {

    GetUserAccessService.returnBrandProfileID().then(function (brandProfileID) {

        SocialMediaUserService.channelProfiles().then(function (channelProfiles) {

            channelProfiles.forEach(function (channel) {

                if (channel.brand_profile_id === brandProfileID && channel.channel_type === 'facebook') {
                    $scope.facebookChannels.push(channel.channel_url);
                    console.log($scope.facebookChannels);
                }

            });
        });
    });
}

EDIT: This is my SocialMediaUserService.channelProfiles service call:

this.channelProfiles = function () {

  var channelProfiles = pullSocialMediaData('list_channel_profiles.json');

  console.log("Channel Profiles Logged: " + channelProfiles);

  return channelProfiles;

}

This is my SocialMediaUserService.returnBrandProfileID service call:

this.returnBrandProfileID = function () {

  var brandProfileID = $q.defer();

  if (angular.isUndefined($sessionStorage.brandProfileID)) {

      GetDataService.getItems('GetUserAccess/' + $cookies.get('authenticationID'))

      .success(function (accessObject) {

          brandProfileID.resolve(accessObject.FusewareID);
      })

      .error(function (error, status) {
          console.error('Fuseware API error: ' + error + ' Status message: ' + status);
      });
  }

  else {
      brandProfileID.resolve($sessionStorage.brandProfileID);
  }

  return brandProfileID.promise;

};

Edit 2: This is the pullSocialMediaData function:

function pullSocialMediaData(url) {

  var userData = $q.defer();

  GetFusionDataService.getItems(url)
    .success(function (data) {
        userData.resolve(data);
    })
    .error(function (error, status) {

    });

  return userData.promise;

}

Thank you!

The SocialMediaUserService.channelProfiles might be designed like this :

this.channelProfiles = function () {

  var channelProfilesPromise = new Promise(function (resolve, reject){

    pullSocialMediaData('list_channel_profiles.json').then(function(result){

        console.log("Channel Profiles Logged: " + result);
        resolve(result);
    });

  });

    return channelProfilesPromise

};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM