0
 angular
    .module('erpos')
    .service('GetBillNoService', ['$http', '$filter', '$cookies', function ($http, $filter, $cookies) {
        return {
            getData: function ($http, $filter, $cookies) {
                var vm = this;

                var billNo = "";
                var date=$filter('date')(new Date(),'yyyy-MM-dd');
                console.log(date);
                date = date.split("-");
                var curDate = date[0].substring(2, 4) + date[1].substring(0, 2);
                HttpService
                    .get('/Company/' + $cookies.get('department') + '/MaterialNo/GetMax')
                    .success(function (data) {
                        switch (data.code) {
                            case 1000:
                                vm.materialIn = data.materialIn;

                                if (vm.materialIn == null) {
                                    vm.billNo = curDate + "0001";
                                } else {
                                    var time = vm.materialIn.maxBillNo.substring(0, 4).toString();
                                    var maxmonth = time.substring(0, 4);
                                    console.log(maxmonth);
                                    if (maxmonth != curDate) {
                                        vm.billNo = curDate + "0001";
                                    }
                                    else {
                                        vm.billNo = (parseInt(vm.materialIn.maxBillNo) + 1).toString();
                                    }

                                }
                                break;
                        }
                    });
                billNo = vm.billNo;
                console.log(billNo);
                return billNo;
            }
        }

    }]);

I want to get a billNo with this service,and then pass the billNo to the controller ,but errors appear that

'TypeError: Cannot read property 'get' of undefined'

I found the error in the 'get' is the property of cookies, but I have injected it already, so who can tell me what is going wrong?

2
  • 1
    HttpService isn't injected Commented Apr 27, 2017 at 7:02
  • I'd like to add that you're still using .success(). I highly suggest switching over to .then(). The first notation is deprecated and is completely removed in AngularJS version 1.6.0. Commented Apr 27, 2017 at 7:16

2 Answers 2

4

change HttpService.get to $http.get will solve your problem.

Sign up to request clarification or add additional context in comments.

9 Comments

I am trying to follow your way to do it,but it still report the same error.
@wqvincent how did you call GetBillNoService .getData?
vm.new_materialIn.billNo=GetBillNoService.getData();
@wqvincent OK, based on the way you call it, you have to change getData: function ($http, $filter, $cookies){...} to getData: function(){...} and use the $http you injected.
thank you for your answer,but now there is another error:I still cannot get the billNo,I console.log the "GetBillNoService.getData()";but it shows that "undefined"
|
0

You are trying to call .get on HttpService. You didn't inject this service on your controller, that is why it is undefined.

Solution 1

Use $http.get instead of HttpService.get, this, is the $http service provided by Angular.

$http.get('/Company/' + $cookies.get('department') + '/MaterialNo/GetMax')

Solution 2

Inject HttpService in your controller to use your own.

.service('GetBillNoService', ['$http', '$filter', '$cookies', 'HttpService', 
                     function ($http,   $filter,   $cookies,   HttpService) {

Comments

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.