NoNameNewbie
NoNameNewbie

Reputation: 11

How to extract response body from Angularjs CacheFactory

I am new to Angularjs and trying to learn cachefactory. I am having trouble reading the response data using cachefactory. As of now I am just trying to print it on console immediately after calling the rest api. My javascript controller is:

(function() {
    "use strict";
    angular
        .module("productManagement")
        .controller("WelcomeCtrl",
            [ '$scope',
                '$http',
                '$timeout',
                "$interval",
                "$cacheFactory",
                WelcomeCtrl ])

    function WelcomeCtrl($scope, $http, $timeout, $interval, $cacheFactory) {
    var onUserComplete = function(response) {

            $scope.fname = response.data.firstName;

            console.log("response.firstName:"+response.data.firstName);

            var $httpDefaultCache = $cacheFactory.get('$http');

            var cachedData = $httpDefaultCache.get('./webapi/details');
            console.log("cachedData print:"+cachedData);

        var onError = function(reason) {
            $scope.error = "Could not fetch the data";
        };

        // GET The values from Rest API
        $http({
            method: 'GET',
            url: './webapi/details',
            cache: true 
        })
        .then(onUserComplete, onError);        

    }
}());

The output from the Webservice is. I can see the output in Network and also can print it on console:

{"id":"1234","firstName":"NoName","lastName":"Newbie"}

When printing the cached data on console it prints out http status code along with few objects:

*cachedData in add training:200,{"id":"1234","firstName":"NoName","lastName":"Newbie"},[object Object],OK*

I am not able to extract the JSON response from the above. I have tried the below but none of them is working:

//console.log("cachedData.data print:"+cachedData.data);
//console.log("cachedData.data.id print:"+cachedData.data.id);
//console.log("cachedResponse print:"+cachedData.response.id);

I have also tried giving the complete URL in $httpDefaultCache.get but it still doesn't work.

Is there any solution to this? I have not copied the entire code to keep it short. Thanks in advance !

Upvotes: 1

Views: 62

Answers (0)

Related Questions