CaptainMorgan
CaptainMorgan

Reputation: 1253

AngularJS $http.post is returning an object

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?

Upvotes: 0

Views: 1439

Answers (1)

Artiom
Artiom

Reputation: 7847

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);
};

Upvotes: 1

Related Questions