質問

Restangular's lack of documentation is killing me. I am trying to consume a REST endpoint. I'm using Django-Tastypie as the backend provider.

var baseQuestions = Restangular.all('questions');
baseQuestions.one($routeParams.questionId).get().then(function(question) {
    console.log(question);
});

And in my AngularJS app config function, I've made sure I set Restangular the correct way.

RestangularProvider.setBaseUrl('/api/v1');
RestangularProvider.setRequestSuffix('?format=json');
RestangularProvider.setResponseExtractor(function(response, operation, what, url) {
    var newResponse;
    if (operation === 'getList') {
        newResponse = response.objects;
        newResponse.metadata = response.meta;
    } else {
        newResponse = response.data;
    }
    return newResponse;
});

But the printed console value shows question is undefined, obviously I don't want that, I need the actual data.

The REST endpoint works fine in the browser.

Can someone please tell me what I'm doing wrong?

役に立ちましたか?

解決

when using Restangular to call for a unique element, I suggest doing it like this:

Restangular.one('questions', $routeParams.questionId).get().then(
    function(question) {
        console.log(question);
    });

Please let me know if it helps.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top