1

I am not able to get the data from resolve in ui-router, its giving me undefined, below is my code

Controller

angular
    .module('App')
    .controller('PostController', PostController);

/** @ngInject */
function PostController(questionData) {
    var vm = this;
        vm.post = questionData;
        console.log("Post Data in Ctrl", questionData);
}

Factory

I have a Cached list of post in bootData, first I check if post with id is in cached array, if found return the post Object, if not make the request and return the promise.

function postService(dataService, $filter, $q) {

    return {
        post : post
    }

    // Cached promise of posts
    var bootData = dataService.boot();

    function post(id) {
        bootData.success(function(res){

                var postList = res.featured.posts.data;
                var post = $filter('filter')(postList, {id: id})[0];

                // Check if post with id found return it
                if (post && post.id > 0) {
                    var deferred = $q.defer();
                    deferred.resolve(post);
                    return deferred.promise;

                } else {
                    // Make request to api and return promise
                    return dataService.get('questions/' + id);
                }
        });
     }
}

Route

resolve: {
           // Check for logged in 
           loginRequired: function(auth){
              return auth.loginRequired();
         },
           postData: function(postService, $stateParams) {
              return postService.post($stateParams.id);
         }
}

I am stuck here, I thing there something wrong in factory method post(id). Please help

3
  • try to return bootData.success from your post method then check for data Commented Nov 17, 2015 at 7:05
  • plz update controller code. Commented Nov 17, 2015 at 7:10
  • factory is giving me the post, i can see post object by logging below this if (post && post.id > 0) { conditional, my controller only taking postData and assigning it to scope vm.post, its returning undefined console.log("Post Data in Ctrl", postData); Commented Nov 17, 2015 at 7:13

1 Answer 1

1

You are returning nothing (equivalent to undefined) from postService.post method. Instead make sure you return promise object return bootData.success:

function post(id) {
    return bootData.success(function(res) {

            var postList = res.featured.posts.data;
            var post = $filter('filter')(postList, {id: id})[0];

            // Check if post with id found return it
            if (post && post.id > 0) {
                return $q.when(post);
            } else {
                // Make request to api and return promise
                return dataService.get('questions/' + id);
            }
    });
 }

Also, avoid using deferred object, in your case you need $q.when.

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

4 Comments

now its returning whole bootData object, I just wanted the single post, How I can return only one post?
Check your logic, filtering part looks correct. Make sure dataService.get('questions/' + id) returns single post.
i want only single post if its cached else single post from server, and ya api is dataService.get('questions/' + id) returning single. currently its returning all thing
So debug it, I indicated you main problem, the rest of your code seems okay. It's something on your end.

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.