goodeggs / angular-cached-resource

An AngularJS module to interact with RESTful resources, even when browser is offline
MIT License
216 stars 29 forks source link

"instance doesn't have any boundParams" error with array #79

Closed nikopolv closed 9 years ago

nikopolv commented 9 years ago

I'm having very mysterious problems with this.

I have 2 API backend urls: http://ktapi.jydev.fi/v2/f/servicedictionary/privateservices/joutsa http://ktapi.jydev.fi/v2/f/servicedictionary/categorystruct/joutsa

Using the exactly same code at factory :

var SidenavFactory = function ($cachedResource, configs) {
return $cachedResource("nav", "http://ktapi.jydev.fi/v2/f/servicedictionary/categorystruct/" +     configs.apiParam, { id: "@id" });
};

SidenavFactory.$inject = ['$cachedResource', 'configs'];

Where configs.apiParam == "joutsa" and id parameter is unused and only there because it's not optional for cached resource.

The first api url works just fine, but when I replace the url with the second without changing anything else it stops working and this error hits console: ngCachedResource 'nav' instance doesn't have any boundParams. Please, make sure you specified them in your resource's initialization, f.e.{id: "@id"}, or it won't be cached.

And the returned array is different. it contains the data (and network shows the call to have returned 200 OK and all data is fine and the JSON validates from both URLs)

image The first object is from the failing url and second is the working one.

What could possbily be causing this?

Thank you!

makebbekus commented 9 years ago

@nikopolv can you paste the API responses in here as well? I wonder if the second API url is failing on the client side because angular-cached-resource is expecting the response objects to have an id field per your resource definition and they don't.

nikopolv commented 9 years ago

@makebbekus You can check the responses from the two links :)

makebbekus commented 9 years ago

Ah, sure enough. The problem is how you're defining the resource. You're telling angular-cached-resource that it can differentiate these resources by a field named id with your third argument { id: "@id" }, but your objects don't have an id field:

[{"mainCategoryId":"public","userFriendlyName":"Julkiset palvelut","subCategories":[{"subCategoryId":"joutsan_kunta","mainCategoryId":"public","userFriendlyName":"Joutsan kunta"},{"subCategoryId":"joutsan_seurakunta","mainCategoryId":"public","userFriendlyName":"Joutsan seurakunta"},{"subCategoryId":"luhangan_kunta","mainCategoryId":"public","userFriendlyName":"Luhangan kunta"}]},{"mainCategoryId":"private","userFriendlyName":"Palvelut","subCategories":[{"subCategoryId":"paivkauppa","mainCategoryId":"private","userFriendlyName":"Päivittäistavaraliikkeet"},{"subCategoryId":"vaatteet","mainCategoryId":"private","userFriendlyName":"Vaatteet & asusteet"},{"subCategoryId":"kayttotavara","mainCategoryId":"private","userFriendlyName":"Elektroniikka, kodinkoneet, ATK, kirjat, valokuvat, urheilu"},{"subCategoryId":"kasityo","mainCategoryId":"private","userFriendlyName":"Käsityötuotteet, kangasliikkeet, lahjat, kukat"},{"subCategoryId":"apteekit","mainCategoryId":"private","userFriendlyName":"Apteekit"},{"subCategoryId":"maatalous","mainCategoryId":"private","userFriendlyName":"Rauta-maatalous, osto- ja myyntiliikkeet"},{"subCategoryId":"majoitus","mainCategoryId":"private","userFriendlyName":"Majoitusliikkeet, mökkien vuokraus, leirintäalueet, juhlatilat, ohjelmapalvelut"},{"subCategoryId":"ruoka","mainCategoryId":"private","userFriendlyName":"Ravintolat, baarit, kahvilat, grillikioskit, pitopalvelut"},{"subCategoryId":"muut","mainCategoryId":"private","userFriendlyName":"Muut palvelut"},{"subCategoryId":"terveys","mainCategoryId":"private","userFriendlyName":"Kauneus, terveys ja liikunta, eläinlääkärit"},{"subCategoryId":"autot","mainCategoryId":"private","userFriendlyName":"Autokaupat, korjaamot, huoltamot"},{"subCategoryId":"kuljetus","mainCategoryId":"private","userFriendlyName":"Liikenne, kuljetus, maanrakennusurakointi, metsäurakointi, taksit"},{"subCategoryId":"omaisuus","mainCategoryId":"private","userFriendlyName":"Omaisuuden hoito, rahoitus, vakuutus, metsäpalvelut, lakiasiat, kirjanpito"},{"subCategoryId":"rakentaminen","mainCategoryId":"private","userFriendlyName":"Rakentaminen ja suunnittelu, kiinteistönhoito, siivous"},{"subCategoryId":"huolto","mainCategoryId":"private","userFriendlyName":"Asentaminen, huolto, korjaus, erikoisammattimiehet"},{"subCategoryId":"teollisuus","mainCategoryId":"private","userFriendlyName":"Valmistus ja teollisuus"},{"subCategoryId":"alkutuotanto","mainCategoryId":"private","userFriendlyName":"Elintarvikkeiden valmistus, alkutuotanto"},{"subCategoryId":"kulttuuri","mainCategoryId":"private","userFriendlyName":"Kulttuuri, lehdet, graafiset tuotteet"},{"subCategoryId":"yhdistykset","mainCategoryId":"private","userFriendlyName":"Yhdistykset"},{"subCategoryId":"vesiosuusk","mainCategoryId":"private","userFriendlyName":"Vesiosuuskunnat"}]}]

You probably want to key these off of mainCategoryId by specifying {mainCategoryId: '@mainCategoryId'}

nikopolv commented 9 years ago

Oh, I thought that id parameter was only used as url parameter since it was needed to have one.

Going to try it out.

Thanks!

johannesjo commented 9 years ago

It seems like the parameter is not optional, am I correct? It sure fails if I dont add any bound params.