1

I have this function in my Angular controller;

        $scope.addPart = function (part) {
        $http.get('stock/' + this.part.oemnumber).success(function (result) {
            $scope.stock = result;
        });
        $scope.woParts.push({"id":part.id, "name":part.partname, "partno":part.partno, "oemnumber":part.oemnumber,
        "stock":part.stock, "price":stock.unitcost});
        $scope.part = '';
    };

It will successfully return the associated stock record and I can get stock.unitcost to output in the console and display in the view. My issue is, I need to push the value onto the woParts array.

I am getting stock undefined with the current syntax. When I try to change it to "price":$scope.stock.unitcost it does not throw an error but still does not work.

What am I missing?

I have also tried to define it before hand but that doesn't work either...

        $scope.addPart = function (part) {
        $http.get('stock/' + this.part.oemnumber).success(function (result) {
            $scope.stock = result;
            $scope.part.price = $scope.stock.unitcost;
        });
        $scope.woParts.push({"id":part.id, "name":part.partname, "partno":part.partno, "oemnumber":part.oemnumber,
        "stock":part.stock, "price":part.price});
        $scope.part = '';
    };

Should I be separating the stock get and the push into 2 functions?

Thanks!

2
  • It should be $scope.stock.unitCost. Can you please log and check the result you are getting from the $http.get call?. Commented Jun 16, 2016 at 18:25
  • Yep, $scope.stock.unitcost is correct and returns the value I want. Answered below - push needs to be moved into http call. Commented Jun 16, 2016 at 18:37

2 Answers 2

1

You are defining $scope.stock inside the success function, which is called async, so the next line where you push, stock is not yet initialized, as it would be initialized after the http call returns. Instead you could do:

$scope.addPart = function (part) {
        $http.get('stock/' + this.part.oemnumber).success(function (result) {
            $scope.stock = result;
            $scope.woParts.push({"id":part.id, "name":part.partname, "partno":part.partno, "oemnumber":part.oemnumber,
        "stock":part.stock, "price":stock.unitcost});
        });

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

1 Comment

Yep, that's it. Thanks, I wrote the 2 bits separately as I was going and should have put them together. Thanks!
1

There's a couple problems here. The first is that you are using stock.unitcost and I think you really want $scope.stock.unitcost. The second problem is that $http.get is asynchronous and the promise will not be resolved before you try to use $scope.stock.unitcost (once you have made that fix). Here is how I believe you will want to refactor your snippet (you need to move the functionality inside of the promise successful resolve function):

$scope.addPart = function (part) {
    $http.get('stock/' + this.part.oemnumber)
        .then(
            function(response) {
                $scope.stock = response.data;
                $scope.part.price = $scope.stock.unitcost;
                $scope.woParts.push({
                    "id":part.id, 
                    "name":part.partname, 
                    "partno":part.partno, 
                    "oemnumber":part.oemnumber, 
                    "stock":part.stock, 
                    "price":part.price});
                $scope.part = '';
            },
            function(error) {
                // handle the error from your $http.get
            });
};

1 Comment

Correct, gaurav5430 just beat you to it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.