2

Alrighty, for an internship project I am trying to create a simple client for a REST API. The current client is using regular angularJS, but I need to use Restangular for this.

I have been searching around a lot, trying out a lot of things, but nothing quite helped me do what I want, or understand what needs to happen.

This is the code snippet that needs to turn into Restangular

$http.get('/rest/programarticle/', {
  'format': 'json'
}).success(function(data, status, headers, config) {
  $scope.articles = angular.articles = data.results;
});

This picks up JSON data which contains some meta data, aswell as an array (results):

{
    "count": 3,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 100,
            "catalog": 7,
            "catalog_alias": "placeHolder",
            "program": 7,
            "name": "placeHolder"
        },
        {
            "id": 92,
            "catalog": 6,
            "catalog_alias": "placeHolder",
            "program": 6,
            "name": "placeHolder"
        },
        {
            "id": 84,
            "catalog": 5,
            "catalog_alias": "string",
            "program": 5,
            "name": "placeHolder"
        }
    ]
}

I have my baseUrl configured correctly, and I tried this out:

var testArticles = Restangular.all('programarticle').getList();
console.log(testArticles);

Using the following ResponseInterceptor:

RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
    var extractedData;
    if (operation === "getList") {
        extractedData = data.results;
        extractedData.count = data.count;
        extractedData.next = data.next;
        extractedData.previous = data.previous;
    } else {
        extractedData = data.data;
    }
    return extractedData;
});

But this still results in something I can't work with in the console:

Promise {$$state: Object, restangularCollection: true, $object: Array[0]}

Which when opened shows the following:

$$state: Object
$object: Array[7]
call: wrapper()
get: wrapper()
push: wrapper()
restangularCollection: true
__proto__: Promise

It does show an array($object), but I cannot reach it using

console.log(testArticles.$object);

Should I be using .get() instead of getList()? If so, how do I use this correctly?

Thank you for your time and please suggest, I am still a beginner.

2
  • 1
    Have you configured Restangular to add the /rest base to the API calls? Why is the data returned from the interceptor not something you can work with? What data are you seeing instead of the expected data? Commented Mar 7, 2016 at 12:45
  • @EzequielMuns Yes I have the base url configured, I will edit my post shortly so it contains the things you just called :) Commented Mar 7, 2016 at 12:50

1 Answer 1

1

What the get/getList functions from restangular return are actually Promises, not the JSON objects retrieved themselves. Promises are a way of abstracting an asynchronous call with a more manageable interface. They stand for an expected future result.

What you need to know is that to get the actual result of the Promise, you must add a callback using the then function, like so:

Restangular.all('programarticle').getList().then(function (articles) {
    $scope.articles = articles;
});

Read more about promises here.

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

3 Comments

Thanks! This already returns something more useful to the console. I had a read on the article, but as the scrub I am I stil don't exactly know how to use this promise and the data in it. Could I ng-repeat for article in articles now? Or do I need something else?
Should be able to ng-repeat on the returned articles array.
Thank you dearly good sir! I should now be able to finally get going.

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.