Closed ylarom closed 7 years ago
I have a social network like app, where each post has a userId referring to the user
var User = sequelize.define('User', { ... }) User.hasMany(models.Post, { as: 'posts', foreignKey: 'userId', onDelete: 'cascade' });
I'm fetching a user with their posts
According to the documentation at http://docs.sequelizejs.com/en/latest/docs/models-usage/ I tried: User.find( { where: { id: userId }, include: [ { association: 'posts' } ] } )
But that results with an error: Cannot read property 'name' of undefined at conformInclude (.../node_modules/sequelize/lib/model.js:291:45)
Looking at the code it seems conformIncludes expects an Association object and not the association name as string
The way it actually works is:
User.find( { where: { id: userId }, include: [ { association: User.associations.posts } ] })
Was I doing it wrong or should the documentation be changed?
I didnt find any posts reference may be because we updated docs. But you should prefer using association object
posts
I have a social network like app, where each post has a userId referring to the user
var User = sequelize.define('User', { ... }) User.hasMany(models.Post, { as: 'posts', foreignKey: 'userId', onDelete: 'cascade' });
I'm fetching a user with their posts
According to the documentation at http://docs.sequelizejs.com/en/latest/docs/models-usage/ I tried: User.find( { where: { id: userId }, include: [ { association: 'posts' } ] } )
But that results with an error: Cannot read property 'name' of undefined at conformInclude (.../node_modules/sequelize/lib/model.js:291:45)
Looking at the code it seems conformIncludes expects an Association object and not the association name as string
The way it actually works is:
User.find( { where: { id: userId }, include: [ { association: User.associations.posts } ] })
Was I doing it wrong or should the documentation be changed?