0

I have a service that adds a new record into the database.

return $http.post('/models/CreateModel', userObj)
                .then(function (res)
                {
                    // Return the new model_id
                    return res.data;
                },
                function (err)
                {
                 //   console.log("THERE WAS AN ERROR");
                });

The value in res.data is the new primary key number. That is returned to the controller as follows:

$scope.newID = ModelService.addModel(xMdl);

When I print out $scope.newID using console.log, I see that it contains an object rather than the value. The object looks like the following in my console:

- d {$$state: Object}
    - $$state: Object
        status:1
        value: 232
      - __proto__: Object
    - __proto__: Object

How do I access the value 232 as I need to update the id in the angular model with this?

3
  • Does it work properly outside of Angular? You can try postman to check it out getpostman.com Commented Aug 5, 2015 at 5:15
  • 2
    This object seems like a promise that $http.post returns... to access the promised value, you need to do .then(function(data){$scope.newID = data}). Commented Aug 5, 2015 at 5:15
  • Yes, it works correctly out of angular. And as you can see, I'm already using the .then function. Commented Aug 5, 2015 at 5:20

1 Answer 1

1

You return prommise in your method

return $http.post('/models/CreateModel', userObj)

in this case you have to retwrite it in next manner:

//your controller
save = function(){
   var self=this;
   this.service.createModel(this.$scope.mmodel)

            .then(function (res)
            {
                self.$scope.neId = res.data;
            },
            function (err)
            {
             //   console.log("THERE WAS AN ERROR");
            });

}

//service  
createModel = function(model){
    return $http.post('/models/CreateModel', userObj);
};
Sign up to request clarification or add additional context in comments.

1 Comment

@user1024941 oops, my fault. fixed createModel function. it didn't return prommise

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.