none
Office.js getFilePropertiesAsync fails on second call in Angular.js RRS feed

  • Question

  • 

    I have an Office Task Pane App written with Office Javascript API (Office.js) that calls Office.context.document.getFilePropertiesAsync and places the returned URL in an angular variable:

    $scope.getDocumentUrl = function () {
    
        Office.context.document.getFilePropertiesAsync(function (asyncResult) {
            $scope.url = asyncResult.value.url;
        });
    };

    I then have a button that calls this. This works file the first time, but when I press the button a second time, it never enters the callback and displays this error:

    TypeError: Object expected
       at verifyAndExtractCall (https://localhost:44304/scripts/office/1.1/o15apptofilemappingtable.js:11:54588)
       at Anonymous function (https://localhost:44304/scripts/office/1.1/o15apptofilemappingtable.js:11:83048)
       at Anonymous function (https://localhost:44304/scripts/office/1.1/o15apptofilemappingtable.js:11:86071)
       at $scope.getDocumentUrl (https://localhost:44304/AngularJs/controllers/sandpit.controller.js:130:6)
       at $parseFunctionCall (https://localhost:44304/AngularJs/bower_components/angular/angular.js:12403:7)
       at callback (https://localhost:44304/AngularJs/bower_components/angular/angular.js:21566:17)
       at Scope.prototype.$eval (https://localhost:44304/AngularJs/bower_components/angular/angular.js:14466:9)
       at Scope.prototype.$apply (https://localhost:44304/AngularJs/bower_components/angular/angular.js:14565:11)
       at Anonymous function (https://localhost:44304/AngularJs/bower_components/angular/angular.js:21571:17)
       at jQuery.event.dispatch (https://localhos


    This is a simplified version of another situation that creates the same error. It also happens with getFileAsync. I know I need $scope.$apply to display the change. I know you can get the URL in other ways. I need to know the cause of the error.

    Wednesday, May 27, 2015 2:19 PM

Answers

  • Hi,

    >> I know I need $scope.$apply to display the change. I know you can get the URL in other ways. I need to know the cause of the error.

    First, I failed to reproduce you issue “TypeError: Object expected”.

    Second, I don’t think it is s good practice to assign the $scope.[value] in an asynchronous function without an angular promise. I recommend using the angular service to wrapper the Apps for Office API in AngularJS application.

    Here is my test code:

    (function () {
        "use strict";
    
        Office.initialize = function (reason) {
    
        };
    })();
    
    var app = (function () {
        return angular.module('demoApp', []);
    })();
    
    
    app.factory('oAppService', ['$q', function ($q) {
        return {
            getUrl: function () {
                var deferred = $q.defer();
    
                Office.context.document.getFilePropertiesAsync(function (asyncResult) {
                    deferred.resolve(asyncResult.value);
                });
    
                return deferred.promise;
            }
        }
    }]);
    
    
    app.controller('homeController', ['$scope', 'oAppService', function ($scope, oAppService) {
        $scope.fileURI = '[file URI]';
    
        $scope.onGetURIClick = function () {
            oAppService.getUrl().then(function (data) {
                $scope.fileURI = data.url;
            });
        };
    }]);

    Regards,

    Jeffrey


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, June 2, 2015 4:25 AM
    Moderator

All replies

  • Hi,

    >> I know I need $scope.$apply to display the change. I know you can get the URL in other ways. I need to know the cause of the error.

    First, I failed to reproduce you issue “TypeError: Object expected”.

    Second, I don’t think it is s good practice to assign the $scope.[value] in an asynchronous function without an angular promise. I recommend using the angular service to wrapper the Apps for Office API in AngularJS application.

    Here is my test code:

    (function () {
        "use strict";
    
        Office.initialize = function (reason) {
    
        };
    })();
    
    var app = (function () {
        return angular.module('demoApp', []);
    })();
    
    
    app.factory('oAppService', ['$q', function ($q) {
        return {
            getUrl: function () {
                var deferred = $q.defer();
    
                Office.context.document.getFilePropertiesAsync(function (asyncResult) {
                    deferred.resolve(asyncResult.value);
                });
    
                return deferred.promise;
            }
        }
    }]);
    
    
    app.controller('homeController', ['$scope', 'oAppService', function ($scope, oAppService) {
        $scope.fileURI = '[file URI]';
    
        $scope.onGetURIClick = function () {
            oAppService.getUrl().then(function (data) {
                $scope.fileURI = data.url;
            });
        };
    }]);

    Regards,

    Jeffrey


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, June 2, 2015 4:25 AM
    Moderator
  • Unfortunately, I am still having the same issue, even when using promises. 

    It's strange you can't reproduce the error, because it happens with completely stripped down code and only the relevant functionality.

    My Office.js version is 1.1 and my Angular version is 1.3.15 if that makes any difference.

    You are right though, I should get into the habit of using promises - I will endeavour to do that from now on.

    Thank you for you help on this matter.

    Regards,

    Paul

    Wednesday, June 3, 2015 9:43 AM