0

I'm writing an app that requires the use of $.getScript quite extensively. I've pushed it into its own function, for ease of use, but now I need to make sure that the function itself is synchronous.

Unfortunately, i am getting error exception: TypeError: Cannot read property 'then' of undefined at n.getMyTask

        var getRequest = function (query) {
            var query = query + listEndPoint;
            var deferred = $q.defer();
            $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
            function execCrossDomainRequest() {
                var executor = new SP.RequestExecutor(appweburl);
                executor.executeAsync({
                    url: appweburl + query + hostweburl + "'",
                    method: "GET",
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "content-Type": "application/json;odata=verbose"
                    },
                    success: function (data) {
                        deferred.resolve(data);
                        console.log(data);
                    },
                    error: function (data, errorCode, errorMessage) {
                        deferred.resolve(data);
                    },
                });
                return deferred.promise;
            };
        };

    var getTaskByUser = function (userID) {
        var query = listEndPoint + "GetByTitle('Asana2')/Items?$select=Title,Category,ID,PriorityCod,Desc2,AssignedTo,CONTRACT_HDR_ID,Status&$filter=Status ne 'compleated' and AssignedTo eq '" + userID + "' &$orderby=Id" ;
        return baseSvcOffice.getRequest(query);
    };

    $scope.getMyTask = function getMyTask() {
        taskSvc.getTaskByUser($scope.LoginUser.status)
            .then(function (response) {
                console.log(response);
        });
    };
1

Your getRequest does not return any promise and that is the reason of your error. Try following code. It may work or not.

var getRequest = function (query) {
    var query = query + listEndPoint;
    var deferred = $q.defer();
    $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
    function execCrossDomainRequest() {
        var executor = new SP.RequestExecutor(appweburl);
        executor.executeAsync({
            url: appweburl + query + hostweburl + "'",
            method: "GET",
            headers: {
                "accept": "application/json;odata=verbose",
                "content-Type": "application/json;odata=verbose"
            },
            success: function (data) {
                deferred.resolve(data);
                console.log(data);
            },
            error: function (data, errorCode, errorMessage) {
                deferred.resolve(data);
            }
        });
    };
    return deferred.promise;
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.