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

Error when validating model with two equal array names #309

Open
thomastvedt opened this issue Sep 18, 2015 · 2 comments
Open

Error when validating model with two equal array names #309

thomastvedt opened this issue Sep 18, 2015 · 2 comments

Comments

@thomastvedt
Copy link

My model looks like this:
Activity

  • Id
  • CategoryIds []
  • Name
  • Task
    • Id
    • CategoryIds['vacation']

When I call model.validate(), I get the following error:
Uncaught TypeError: Cannot use 'in' operator to search for '0' in vacation

Is this because I have two attributes named "CategoryIds" in my model? One attribute "activity.CategoryIds[]" and one nested attribute "activity.Task.CategoryIds[]". Maybe flattening isn't working? Is this a bug, and can it be fixed? :-) Or am I doing something wrong? (validation works on simpler models).

trace:
Uncaught TypeError: Cannot use 'in' operator to search for '0' in vacationb.each.b.forEach @ underscore-min.js:20Backbone.Validation.flatten @ backbone-validation.js:82(anonymous function) @ backbone-validation.js:86b.each.b.forEach @ underscore-min.js:20(anonymous function) @ backbone-validation.js:85b.each.b.forEach @ underscore-min.js:20Backbone.Validation.flatten @ backbone-validation.js:82(anonymous function) @ backbone-validation.js:90b.each.b.forEach @ underscore-min.js:20Backbone.Validation.flatten @ backbone-validation.js:82Backbone.Validation.options.validate @ backbone-validation.js:285Page.extend.save @ ActivityEditor2.js:95jQuery.event.dispatch @ jquery-1.9.1.js:3074jQuery.event.add.elemData.handle @ jquery-1.9.1.js:2750

@platinumazure
Copy link

Can you please include your model definition including its validation
block?
On Sep 18, 2015 5:46 AM, "Thomas Tvedt" notifications@github.com wrote:

My model looks like this:
Activity

  • Id
  • CategoryIds []
  • Name
  • Task
    • Id
    • CategoryIds['vacation']

When I call model.validate(), I get the following error:
Uncaught TypeError: Cannot use 'in' operator to search for '0' in vacation

Is this because I have two attributes named "CategoryIds" in my model? One
attribute "activity.CategoryIds[]" and one nested attribute
"activity.Task.CategoryIds[]". Maybe flattening isn't working? Is this a
bug, and can it be fixed? :-) Or am I doing something wrong? (validation
works on simpler models).

trace:
Uncaught TypeError: Cannot use 'in' operator to search for '0' in
vacationb.each.b.forEach @ underscore-min.js:20Backbone.Validation.flatten
@ backbone-validation.js:82(anonymous function) @
backbone-validation.js:86b.each.b.forEach @ underscore-min.js:20(anonymous
function) @ backbone-validation.js:85b.each.b.forEach @
underscore-min.js:20Backbone.Validation.flatten @
backbone-validation.js:82(anonymous function) @
backbone-validation.js:90b.each.b.forEach @
underscore-min.js:20Backbone.Validation.flatten @
backbone-validation.js:82Backbone.Validation.options.validate @
backbone-validation.js:285Page.extend.save @
ActivityEditor2.js:95jQuery.event.dispatch @
jquery-1.9.1.js:3074jQuery.event.add.elemData.handle @ jquery-1.9.1.js:2750


Reply to this email directly or view it on GitHub
#309.

@thomastvedt
Copy link
Author

The error appears even though there is no validation-block defined. (when I call model.set(), which calls .validate() etc.)

This is what my Activity-model looks like while debugging (js-code is minimal):

activityandtasksameid

it fails in "flatten(obj, into, prefix) backbone-validation.js:78

var flatten = function (obj, into, prefix) {
into = into || {};
prefix = prefix || '';

_.each(obj, function(val, key) {
  if(obj.hasOwnProperty(key)) {
    if (!!val && _.isArray(val)) {
      _.forEach(val, function(v, k) {
        flatten(v, into, prefix + key + '.' + k + '.');
        into[prefix + key + '.' + k] = v;
      });
    } else if (!!val && typeof val === 'object' && val.constructor === Object) {
      flatten(val, into, prefix + key + '.');
    }

    // Register the current level object as well
    into[prefix + key] = val;
  }
});

return into;
};

When flattening Task.CategoryIds, _.isArray(key) is true.. then there is a recursive call to:
flatten(obj="con", into=.., prefix="Task.CategoryIds.0."){

now on line 82 it fails, because obj is a string and not an array. obj = "con":
_.each(obj, function(val, key) {

Uncaught TypeError: Cannot use 'in' operator to search for '0' in con

Activity.Task.CategoryIds is an array of strings. Is there something wrong with my model?

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

No branches or pull requests

2 participants