Collection REST + JSON


#1

Hi,

I’m playing around with retrieving large sets of data with REST.

What I’m getting back is a JSON formatted data set (list of items). I’m wondering how to push it into a Collection, do I even need to do that to get one specific item from the list based on ID let’s say. Would I do it in the REST node? Do I do it outside?

Let’s say what I’m getting back is
[ id, name, age ] and I get 20 items with the same formatting.

Thanks!


#2

I would do it in the REST node itself, in the response function. Like this:

response:function(outputs,response) {
        if(response.status !== 200) return;

	//assuming the response look like { items: [] }
	var items = response.content.items;
	var collection = Noodl.Collection.get('My Collection');
	        
        for(var i=0; i<items.length; i++) {
            collection.add(Noodl.Model.create(items[i]));
        }
}

You can also send the collection as an input to the REST node instead of hardcoding it like I did


#3

Thanks for the code! It helped quite a bit.
Since I didn’t know what I’m getting back as data at all, this was a good starting point.

It just occurred to me that I can do a console.log for the ‘response.content’ and look how the data is being formatted. As my JSON was [ {item1, item2, etc.} ] I ended up using this:

var items = response.content; // array of content objects
console.log(items);

var collection = Noodl.Collection.get('My Collection');

for(var i=0; i<items.length; i++) {
    collection.add(Noodl.Model.create(items[i]));
    // preview a possible model structure
    if (i === 1) {
        console.log(items[i]);
    }
}

Again, thanks a ton! Loving the simplicity of working with data.


#4

So here’s one more unexpected behaviour I’m getting.

My prototype is working 100% fine in the Noodl editor, but as soon as I open it in a browser window, console shows "REST: Failed to request – … followed by the link where I’m getting the data from. And no data gets displayed of course.

I’ve only tried this with 2 different online data sources, but error message is the same.

Thanks!


#5

Solution for others in for Safari (OSX):

Enable the developer menu, and select “Disable Cross-Origin Restrictions” from the develop menu.
Perhaps a small detail to add to the documentation for noobs like me :wink:

Doh…


#6

Another option is to prepend http://cors-anywhere.herokuapp.com/ to all your URLs, it’ll work as a proxy and remove the CORS restriction. Note that all your data will flow through that server though, and I wouldn’t rely on it for anything important. But for quick prototypes and explorations it works well, and it’ll enable the prototype to run in all browsers, cordova, etc.


#7

Hi, I’ve just started with noodle, and it looks like a marvelous tool.

I’ve the same problem as @Regi.
I’m getting a json array with items, but I don’t know how to use them.
I can get specific items from my list, by directly setting my output parameter to response.content[2].
But I don’t know how to go from “json Array” to “Collection” to “Scroll view items”.

I need more clues than @Regi needed :slight_smile:

All help is appreciated,

Fedor