caio-ribeiro-pereira / building-apis-with-nodejs

Some stuffs of the book Building APIs with Node.js
140 stars 65 forks source link

TypeError: db.models[key].associate is not a function #15

Closed Orinameh closed 5 years ago

Orinameh commented 7 years ago

Hi, thanks for your book. I have been battling with this error TypeError: db.models[key].associate is not a function. I have tried to resolve but all to no avail. Can you help out?

caio-ribeiro-pereira commented 7 years ago

@Orinameh you should use the book's version of sequelize, because the new version will break in this part. Take a look: http://docs.sequelizejs.com/manual/tutorial/upgrade-to-v4.html

ultrasamad commented 6 years ago

Yh, I also faced the same problem. I was able to install the book's version of sequelize, but for the sqlite3, the book's version ie 3.1.1 couldn't be installed. So I installed the current version which is 3.1.13. Things seem to be working for now, but I fear things might break in the subsequent chapters.

aidooyaw1992 commented 6 years ago

@ultrasamad im sticking to your opinion , i hope it doesnt break in the upcoming chapters. It is working now

ultrasamad commented 6 years ago

Yh...If it does then there is no need uninstalling the current version, just maintain it but rather tweak the old codes to use the new codes of that version.

renathoaz commented 6 years ago

Hi there, This problem seems to me that the associate property is not being declared, if you not declare It in one of your models, do:

Object.keys(db.models).forEach(key =>{
    if (db.models[key].hasOwnProperty('associate')){
        db.models[key].associate(db.models);
    }
});
iagofrota commented 6 years ago

Could you put the template code where you're giving the error?

lvs-training commented 6 years ago

Got the same problem, look like this works... for now :)

Object.keys(db.models).forEach(key => {
    db.models[key].options.classMethods.associate(db.models);
});
jeffnyman commented 6 years ago

I can confirm that the solution by @lvs-training works. I would definitely not recommend going back versions. It's better to update code accordingly but maybe have branches for situations where major differences occur in dependencies.

Entirely separately, but related, this also occurs with Jwt at one point. You can't do ExtractJwt.fromAuthHeader() anymore. (This is in auth.js.) Instead you have to do ExtractJwt.fromAuthHeaderWithScheme("jwt").

So even if not different branches, perhaps just updating the README with known discrepancies for dependency updates might be worthwhile. So far the one listed here and the one I just brought up are the only functional ones I've encountered. There is another issue where you might want to add "operatorAliases: false" to config.js to handle a deprecation warning with Sequelize.

vserpa commented 5 years ago

Use ES6 classes with Sequelize 4

Model.associate = function (models) {
    Tasks.belongsTo(models.Users);
};

Look here

caio-ribeiro-pereira commented 5 years ago

Guys, many changes happened in the Sequelize module, unfortunatelly I can't update the book, so you have to use the right (old) versions of all modules in the book to the project runs fine.

robsonsky commented 5 years ago

Ok guys, so I've faced the same problem and this is the solution:

OLD const Users = sequelize.define('Users', { ... }, { classMethods: { associate: (models) => { Users.hasMany(models.Tasks); }, isPassword: (encodedPassword, password) => { return bcrypt.compareSync(password, encodedPassword); } });

NEW remove the classMethods object and place the following after:

Users.associate = function (models) { Users.hasMany(models.Tasks); };

Users.isPassword = function(encodedPassword, password) { return bcrypt.compareSync(password, encodedPassword); }

return Users;

this book is an amazing piece of work, hope you all enjoy it :)