0

Loading json using $http isn't working. Can't figure out where the problem is

.factory('streamStore', function ($http) {
    var cachedData = null;

    return {
        findAll: function getAll() {
            $http.get('img/sample.json').
                success(function (data, status, headers, config) {
                    console.log("inside stream controller");
                    cachedData = data; return cachedData;
                }).
                error(function (data, status, headers, config) {
                    // log error
                });
        }

And in my controller this is how I'm calling it:

.controller('streamCtrl', function ($scope, ,streamStore) {
    $scope.streams = streamStore.findAll();//not working ,
2
  • show the error msz ? Commented Jul 16, 2015 at 13:58
  • why not just returning data; Commented Jul 16, 2015 at 14:00

2 Answers 2

1

There is a error in your code . Check below

.factory('streamStore', function ($http) {

 var cachedData = null;

 return {
    findAll: function() {
        $http.get('img/sample.json').
            success(function (data, status, headers, config) {
                console.log("inside stream controller");
                cachedData = data;
                return cachedData;
            }).
            error(function (data, status, headers, config) {
                // log error
            });
    }

and then call , as you were calling in your controller.

or use this

.factory('streamStore', function ($http) {

 var cachedData = function(){
     return $http.get('img/sample.json');
 };

 return {
    findAll: function(){
        return cachedData();
       }
    }

});

then in controller

streamStore.findAll().then(function(response){
     console.log(response) // here is your data
})
Sign up to request clarification or add additional context in comments.

3 Comments

i use the above method, replace the console log with $scope.streams i get a error -TypeError: streamStore.findAll(...).then is not a function at new <anonymous> (app.js:34) at Object.invoke (ionic.bundle.js:13012)
tanks ,second method works can i pass argument to cachedata(userid) say to fetch spefic user id return cacheData(userid) along with the controller streamStore.findAll(userid)
yes you can do that and change your request type accrodingly
1

since you have not returned anything from factory.

.factory('streamStore', function ($http) {
    var cachedData = null;

    return {
        findAll: function getAll() {
            $http.get('img/sample.json').
                success(function (data, status, headers, config) {
                    console.log("inside stream controller");
                    cachedData = data; return cachedData;

                     // should return something 
                     return cachedData
                }).
                error(function (data, status, headers, config) {
                    // log error
                });
        }

one more suggestion return promise to make it safe .

return $http.get('img/sample.json')...

and handle with then in controller.

.controller('streamCtrl', function ($scope, ,streamStore) {
   streamStore.findAll().then(function(res){
         //res will contain your data 
         $scope.streams =  res.data;
     });

1 Comment

thanks for your suggesttion , the problem is im returning alot of function using the return { findAll,post,findOne ...} do u get the idea ,so i can call to any of the function in the controller ;

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.