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
Model.getX: Cannot read property 'map' of undefined. #4157
Comments
That code should only execute if options.include is set. Can you post model definitions + assocations as well? |
User.jsmodule.exports = function(sequelize, DataTypes) {
var user = sequelize.define("user", {
id: {
type: DataTypes.INTEGER,
field: 'emplid',
primaryKey: true,
autoIncrement: false
},
firstname: {
type: DataTypes.STRING,
field: 'firstname_preferred',
defaultValue: '',
allowNull: false
},
middlename: {
type: DataTypes.STRING,
field: 'middlename_preferred',
defaultValue: '',
allowNull: false
},
lastname: {
type: DataTypes.STRING,
field: 'lastname_preferred',
defaultValue: '',
allowNull: false
},
legalFirstname: {
type: DataTypes.STRING,
field: 'firstname_legal',
allowNull: false
},
legalMiddlename: {
type: DataTypes.STRING,
field: 'middlename_legal',
defaultValue: '',
allowNull: false
},
legalLastname: {
type: DataTypes.STRING,
field: 'lastname_legal',
allowNull: false
},
username: {
type: DataTypes.STRING,
field: 'username',
allowNull: false,
unique: true
},
email: {
type: DataTypes.STRING,
defaultValue: '',
allowNull: false
},
phone: {
type: DataTypes.STRING,
defaultValue: '',
allowNull: false
},
location: {
type: DataTypes.STRING,
field: 'location',
allowNull: false
},
photo: {
type: DataTypes.STRING,
field: 'photo_url',
allowNull: false
}
}, {
//tableName: 'people',
classMethods: {
associate: function(models) {
user.belongsToMany(models.group, {
foreignKey: "emplid",
through: 'userGroup',
onDelete: 'cascade'
});
user.belongsToMany(models.role, {
scope: {
roleid: {$ne: 11}
},
foreignKey: "emplid",
through: 'user_roles',
onDelete: 'cascade'
});
}
}, defaultScope: {
include: [
// 'as' clauses prevent table alias errors
{ model: sequelize.models.role, as: sequelize.models.role.tableName },
{ model: sequelize.models.group, as: sequelize.models.group.tableName }
]
}, scopes: {
facstaff: {
include: [
{ model: sequelize.models.role, as: sequelize.models.role.tableName, where: {id: {$lte: 2}} },
{ model: sequelize.models.group, as: sequelize.models.group.tableName }
]
}
}
});
return user;
}; Group.jsmodule.exports = function(sequelize, DataTypes) {
var group = sequelize.define("group", {
id: {
type: DataTypes.INTEGER,
field: 'groupid',
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
description: {
type: DataTypes.STRING
},
longDescription: {
type: DataTypes.STRING(1000),
field: 'long_description'
}
}, {
classMethods: {
associate: function(models) {
group.belongsToMany(models.user, {
foreignKey: "groupid",
through: 'userGroup',
onDelete: 'cascade'
});
}
}
});
return group;
}; |
I forgot to show the userGroup model: UserGroup.jsmodule.exports = function(sequelize, DataTypes) {
var userGroup = sequelize.define("userGroup", {
title: {
type: DataTypes.STRING,
field: 'title',
allowNull: false,
defaultValue: ''
}
}, {
tableName: 'user_groups'
});
return userGroup;
}; Edit: The Plot ThickensThis works in the reverse order: I.e., if I get a single Group, then call getUsers, I get exactly what I expect. I suspect scopes or the manually defined join table |
Yep, definitely related to scopes somehow - not sure why :) But it seems we are forgetting to call |
Huh, my lib code there looks the same as yours. addOptionalClassMethods.call(this);
this.$scope = _.isPlainObject(this.options.defaultScope) ? this.options.defaultScope : {};
_.each(this.options.scopes, function (scope) {
if (_.isPlainObject(scope) && scope.include) {
conformOptions(scope);
}
}); |
Yes, I was merely linking to the line that needs to be fixed for this to work :-) |
Works like a charm! Thanks @janmeier and team! |
Sequelize Ver: 3.3.2
Model
user
has an N:M association with modelgroup
. Attempting to usegetGroups()
results in an error.Source
Error:
The code referenced by the first line of the stack trace is:
Which suggests that Sequelize is looking for includes and is not finding them. Am I using
getX()
incorrectly?The text was updated successfully, but these errors were encountered: