alekseykulikov / backbone-offline

[Deprecated] Allows your Backbone.js app to work offline
MIT License
720 stars 56 forks source link

Uncaught TypeError: Cannot call method 'toString' of undefined #29

Open bredfield opened 11 years ago

bredfield commented 11 years ago

I'm getting an "Uncaught TypeError: Cannot call method 'toString' of undefined" when I try to fetch a collection with an initialized storage object.

It's originating from line 356: if (!_.include(this.storage.destroyIds.values, item.id.toString())) {

The API I'm using is REST based, and each model has an ID...do you know what else could be causing the problem? Any insight would be greatly appreciated; really looking forward to using this.

this-chris commented 11 years ago

Did you have a ID in your response? When not - is that the problem why it throws a error. It needs a identifier to save the object into the localStorage.

models should have id primary key. If you use MongoDB you should change response replaced _id to id

Regards, Chris

bredfield commented 11 years ago

That's what I thought at first, but it seems as if all models have valid IDs (and I'm not using Mongo). Weird, I keep fooling around with it.

On Friday, January 25, 2013 at 4:33 AM, Chris wrote:

Did you have a ID in your response? When not - is that the problem why it throws a error. It needs a identifier to save the object into the localStorage.

models should have id primary key. If you use MongoDB you should change response replaced _id to id

Regards, Chris

— Reply to this email directly or view it on GitHub (https://github.com/Ask11/backbone.offline/issues/29#issuecomment-12693781).

ghost commented 11 years ago

Problem here is that the ID coming from the server may be out of your control. Backbone allows you to specify idAttribute as a property on your model to tell it what the model.id value should correspond to. I would suggest instead of assuming id is always there, check for it, and if it is not there, check if the idAttribute key is present and get the value for that, otherwise return an empty string.