Question

The following code fails when invoking a IBM Worklight from angular service

My angular service is invoking a worklight adapter

 .factory('Profile', [
               function () {
               return {

               loadUserProfileSuccess:function(result){
                   if(result.statusCode = 200)
                      console.log("Succes");

               },

               loadUserProfileFailure:function (result){
                        console.log("failure");

               },
                  getUserProfile: function($rootScope,user){

                 var invocationData = {
                   adapter : 'RSSReader',
                   procedure : 'find_or_create',
                   parameters : [user.name,user.email]
                 };

                 WL.Client.invokeProcedure(invocationData,{
                    onSuccess : loadUserProfileSuccess,
                    onFailure : loadUserProfileFailure
                          });
               },
            }//end return

 }]);

When invoking the worklight adapter, it appears everything works great on the procedure side. But angular complains even though the callback function loadUserProfileSuccess is defined...

Error: Can't find variable: loadUserProfileSuccess
getUserProfile@file:///...
Was it helpful?

Solution

You are trying to access loadUserProfileSuccess incorrectly. Move the functions inline:

WL.Client.invokeProcedure(invocationData, {
  onSuccess: function (result) {
    if (result.statusCode = 200) console.log("Succes");
  },
  onFailure: function (result) {
    console.log("failure");
  }
});

Or declare them outside of the object you're returning:

factory('Profile', [

function () {
    var loadUserProfileSuccess = function (result) {
        if (result.statusCode = 200) console.log("Succes");

    };
    var loadUserProfileFailure = function (result) {
        console.log("failure");

    };
    return {

        loadUserProfileSuccess: loadUserProfileSuccess,

        loadUserProfileFailure: loadUserProfileFailure,
        getUserProfile: function ($rootScope, user) {

            var invocationData = {
                adapter: 'RSSReader',
                procedure: 'find_or_create',
                parameters: [user.name, user.email]
            };

            WL.Client.invokeProcedure(invocationData, {
                onSuccess: loadUserProfileSuccess,
                onFailure: loadUserProfileFailure
            });
        },
    } //end return


}]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top