jasonkneen / RESTe

A simple JavaScript REST / API helper for Titanium

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

undefined is not an object (evaluating 'collectionConfig.read')

adamtarmstrong opened this issue · comments

I am getting a weird RESTe error.
ERROR:
message = undefined is not an object (evaluating 'collectionConfig.read')
sourceURL = ........./reste.js
stack = ....../alloy/controllers/main.js:55:50

API CONFIG:

Alloy.Globals.postman.config({
    debug: true,
    autoValidateParams: false,
    errorsAsObjects: false,
    timeout: 15000,
    url: "https://api.getpostman.com/",
    requestHeaders: {
    	"Content-Type": "application/json",
    	"X-Api-Key": "*****removed******"
    },
    models: [{
        name: "name",
        id: "id",
        //content: "response",
        collections: [{
            name: "queryPostmanCollections",
            content: "collections",
            read: "getPostmanCollections"
        }]
    }],
    methods: [{
        name: "getPostmanCollections",
        get: 'collections/',
        onError: function(e, retry){
        	Ti.API.info("Error getting Collections:\n" + JSON.stringify(e));
       },
       onLoad: function(e, callback) {
	        callback(e);
	    }
    }]
});

MAIN.JS

Alloy.Collections.queryPostmanCollections.fetch();

Please test with 1.5.2 -- Believe it's an issue with "content" not being specified

Still same issue with 1.5.2. Is it possible there is an issue/conflict with the collections.content property being called "collections"?

......
collections: [{
            name: "queryPostmanCollections",
            content: "collections",
            read: "getPostmanCollections"
        }]

Maybe I can do a transform on that. I cant control the array name being returned in the API from Postman. hmm...

Looking at reste -- that line is used ones (.read) and before it we do:

var collectionConfig = _.where(modelConfig.collections, {
                name: model._name
                })[0];

so wondering if that is the problem

Well....a transform here won't work. It is failing on that "reserved" word before the API Response is ever returned - so I can't transform in the onLoad method.

And I can't imagine how beforePost or beforeSend would help either.

Any other creative ideas, or hooks/methods, I should try?

OK. I traced down this issue. I replicated the exact endpoint name and response data into mockable.io and it worked fine. There are NO issues in RESTe with using the words 'collections' in the collections.content or in the endpoint. Those work fine.

The reason it was failing is because Postman is using custom Request Origin headers in their Native App to prevent use in browsers or any other app - unless the origin matches some obscure value they are looking for to prevent Cross-Site (CORS).

I never thought it was an API Access issue because I assumed if it were I would see that in the 'onError'. But, at least with Postman, due to the invalid origin, they are returning 'http 0' and I guess RESTe didnt know how to handle an error code of 'http 0'.

As always, thanks for this awesome tool!
Adam