Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: existing.set is not a function #4224

Closed
PeterW570 opened this issue Oct 8, 2019 · 3 comments
Closed

TypeError: existing.set is not a function #4224

PeterW570 opened this issue Oct 8, 2019 · 3 comments

Comments

@PeterW570
Copy link

const collection = new Backbone.Collection();
const model = new Backbone.Model({
    id: 'hasOwnProperty'
});
collection.set([model]); // TypeError: existing.set is not a function

In Collection#set, it does: var existing = this.get(model); which in this case is setting existing to Object#hasOwnProperty rather than undefined because Collection#get is returning the result ofthis._byId['hasOwnProperty']

Have reproduced this bug in a codepen here.

@paulfalgout
Copy link
Collaborator

This seems like an odd thing to fix. This to me seems unlikely in general, but fixing it would add at least some cost to most actions across backbone where this issue is never likely to arise. If anything the best fix to me seems to be something like adding to the docs "ids cannot be members of Array.prototype.String"

@PeterW570
Copy link
Author

Can also cause the same bug by setting it to a different attribute and then setting idAttribute of the model to that, e.g:

  const keyedModel = Backbone.Model.extend({ idAttribute: 'key' });
  const keyedModelCollection = Backbone.Collection.extend({ model: keyedModel });
  const collection = new keyedModelCollection();
  const model = new keyedModel({ key: 'hasOwnProperty' });
  collection.set([model]); // TypeError: existing.set is not a function

Agree it's an edge case, but I think it's a valid use case if the string is a valid unique identifier in the database

@jgonggrijp
Copy link
Collaborator

As far as I'm concerned, this isn't specific to Backbone; it's on the list of things you should do nowhere in JavaScript. Of course, it's fine if you learn by experimentation that doing stuff like this breaks things, but this doesn't need documentation in Backbone, let alone a fix.

As for unique identifiers in databases, I expect those to be serial numbers or something more advanced like a UUID. Certainly not names of JavaScript's global object methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants