get-function returns undefined

30 views
Skip to first unread message

Helene Francke

unread,
Sep 14, 2017, 12:36:00 PM9/14/17
to KnockoutJS
Hi!

I've got a list of objects (an observable array) and I would like to click on an object and get a more detailed version of that object, kind of like the single page application tutorial where there is a list of e-mails and clicking one clears the list and shows the email. The problem is that the api-call (via $.get()) returns undefined. I've tried the api in Postman and I get the expeted data there, but when I click an item I get nothing. The rest of the function triggers fine (the array is cleared and the console logs are printed in the console and I get no errors), but I'm not receiving any data.

Knockout.js:

 //Data
    self.itemList = ko.observable();
    self.itemId = ko.observable();
    self.chosenItemData = ko.observable();

    //Behaviours
    self.GetAllItems = function () { //Search function, hence the params. It works just fine.
        var params = $('#params').val();
        var url = '/api......./' + params;
        $.get(url, self.itemList);
    }; //GetAllItems

    self.goToItem = function (item) { //This one is the issue. Why isn't it returning the expected data? It is triggered by a click event.
        self.itemId(item.Id);
        self.itemList(null);
        var url = 'api/.....' + item.Id+ '/';
        console.log(url);
        $.get(url, self.chosenItemData);
        
    };

HTML:

<div data-bind="with: chosenItemData">
            <img class="card-img--size" data-bind="attr: { src: $data.ImageUrl }" onerror="onItemImageError(this)" />
            <strong class="margin-left--m"><span data-bind="text: $data.Name"></span></strong>
        </div>


Very grateful for any ideas on why it isn't working and ways to solve it.

//Helene

Helene Francke

unread,
Sep 15, 2017, 3:26:38 AM9/15/17
to KnockoutJS
Ok, I found the problem. I had missed an opening / in the API-url for the goToItem-function. Sleep really is important when working with code. 

Jean-Sebastien Binette

unread,
Sep 16, 2017, 8:02:07 AM9/16/17
to KnockoutJS
in my experience; 99% of issues can be solved easily the next day or the next week :-)
Reply all
Reply to author
Forward
0 new messages