AngularJs Separate function under factory

I am using AngularJs and backend CodeIgniter REST API .Here is the sample of code

data.js

`
app.factory("Data", ['$http', 'toaster','$log',
    function ($http, toaster,$log) { 

// This service connects to our REST API

        var serviceBase = 'link';

        var obj = {};
        obj.toast = function (data) {
            toaster.pop(data.status, "", data.message, 10000, 'trustedHtml');
        }
        obj.get = function (q) {
            return $http.get(serviceBase + q).then(function (results) {
                return results.data;
            });
        };
        obj.post = function (q, object) {
            //$log.log(object.customer);
            $http({
                 method: 'POST',
                 url: serviceBase + q,
                 headers: {
                        Accept: "application/json",
                        "REST-API-KEY": "key"
                 },
                 data: object.customer

            })
            .then(function (results) {
                $log.log(results);
                return results.data;
            });
        };
        obj.put = function (q, object) {
            return $http.put(serviceBase + q, object).then(function (results) {
                return results.data;
            });
        };
        obj.delete = function (q) {
            return $http.delete(serviceBase + q).then(function (results) {
                return results.data;
            });
        };

        return obj;
}]);

authctrl.js

app.controller('authCtrl', function ($scope, $rootScope, $routeParams, $location, $http, Data,sData) {
    //initially set those objects to null to avoid undefined error
    $scope.login = {};
    $scope.signup = {};
    $scope.doLogin = function (customer) {
        Data.post('login', {
            customer: customer
        }).then(function (results) {
            Data.toast(results);
            if (results.status == "success") {
                $location.path('dashboard');
            }
        });
    };
    $scope.signup = {email:'',password:'',name:'',phone:'',address:''};
    $scope.signUp = function (customer) {
        sData.post('signUp', {
            customer: customer
        }).then(function (results) {
            Data.toast(results);
            if (results.status == "success") {
                $location.path('dashboard');
            }
        });
    };
    $scope.logout = function () {
        Data.get('logout').then(function (results) {
            Data.toast(results);
            $location.path('login');
        });
    }
});

It is having an error which is

TypeError: Cannot read property 'then' of undefined
    at h.$scope.doLogin (authCtrl.js:8)
    at $a.functionCall (angular.min.js:172)
    at angular.min.js:189
    at h.a.$get.h.$eval (angular.min.js:108)
    at h.a.$get.h.$apply (angular.min.js:109)
    at HTMLButtonElement. (angular.min.js:189)
    at angular.min.js:31
    at q (angular.min.js:7)
    at HTMLButtonElement.c (angular.min.js:31)

And also as you can see i am using single post method for both login and signUp function .So what i want to make it separate function like

For Login


    obj.post = function (q, object) {
                //$log.log(object.customer);
                $http({
                     method: 'POST',
                     url: serviceBase + q,
                     headers: {
                            Accept: "application/json",
                            "REST-API-KEY": "3e9aba65f0701f5e7c1c8a0c7315039e"
                     },
                     data: object.customer

                })
                .then(function (results) {
                    $log.log(results);
                    return results.data;
                });
            };

For Sign up


    obj.post = function (q, object) {
                //$log.log(object.customer);
                $http({
                     method: 'POST',
                     url: serviceBase + q,
                     headers: {
                            Accept: "application/json",
                            "REST-API-KEY": "3e9aba65f0701f5e7c1c8a0c7315039e"
                     },
                     data: object.customer

                })
                .then(function (results) {
                    $log.log(results);
                    return results.data;
                });
            };

How to write it in angularJs?

Upvotes: 1

Views: 291

Answers (1)

Nishant
Nishant

Reputation: 5109

because you are returning nothing from Data.post, instead of returning the promise .

i.e. change your Data.post to

  obj.post = function (q, object) {
        //$log.log(object.customer);
        return $http({
             method: 'POST',
             url: serviceBase + q,
             headers: {
                    Accept: "application/json",
                    "REST-API-KEY": "key"
             },
             data: object.customer
        });            
    };

Upvotes: 2

Related Questions