0

I know the problem is the order of execution, the scope fires async and loads the data after the rest of the code has been processed. I have a httpGET that gets the information from a web service and it's inside my Facebook share function when the user clicks the Facebook share button. But i tried adding a watch and .then with return promise in the httpget but both i could not get to work.

So the idea is the following: I have an CDImage that the user shares on facebook and i have another directory that holds promotional images for that same CD, not all of them so the httpGET checks if the promotionCDid exists if it exists the variable CDImage should be updated with the CDPromotionalURL instead of the standard url is get from CDImage so the user shares the Promotional image instead of the default CD cover.

So far the problem is that the CDImage does not change directly and console.log(CDImage) displays the CDCover the first time and when you click the button after several seconds the CDImage shows the CDPRomotionURL image.

var CDPromotionalImageUrl = "EMPTY";

 $('#facebookshare').click(function () {

    $scope.GetCDdata = function () {

        $http({
            method: 'Get',
            url: "/GetCDPromotionImage?id_CD=" + promotionCDId,
        })
            .success(function (data, status, headers, config) {
                $scope.CDdata = data;
                CDPromotionalImage = $scope.CDdata[0].filename
                $scope.CDPromotionalImageUrl = "https://website.com/" + CDPromotionalImage          
            })
            .error(function (data, status, headers, config) {
                $scope.message = 'Unexpected Error';
            });
    };

        if (CDPromotionalImageUrl == "EMPTY") {
            CDImage = CDImage;
        } else {
            CDImage = CDPromotionalImageUrl;
        }

        console.log(CDImage)

    var $this = $(this);
    var urlShare = (window.location.href);
    var obj = {
        method: 'share',
        href: (urlShare),
        picture: (CDImage),
        title: 'The ' + CDName,
        caption: "As heard on website.com",
        description:(description)
    };
    function callback(response) {
        //alert("Post ID: " + response['post_id']);
    }

    FB.ui(obj, callback);

 });
$scope.GetCDdata();
3
  • The $http service executes asynchronously, which implies that the succes / error callbacks won't ever be executed until the current thread of execution first completes. Since you are printing the CDPromotionalImageUrl & CDImage variables to the console synchrnously as part of the current execution thread, they won't have a value. Commented Dec 16, 2016 at 11:45
  • Printing these variables as part of a watch routine may be an option - the $http roundtrip operation will trigger a digest after succeeding / erroring, and watches involving changes will then run. Or you could put any logic requiring the result of the HTTP operation directly in the success callback. Commented Dec 16, 2016 at 11:49
  • Hi IAmDranged, i kinda got to that conclusion as well. I tried adding the complete facebook function (the last part from var $this) into the httpGET success section but the sharebutton does not work anymore in that case. Commented Dec 16, 2016 at 11:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.