I am trying to update my code to post the latest Sequelize updates. I use
Sequelize: 4.2.0
Node: 7.10.0
NPM: 5.0.3
Problem
I cannot configure the user model correctly. I have implemented some instance methods that don't seem to work. The class should not be created properly.
user.js
module.exports = (sequelize, DataTypes) => {
var User = sequelize.define('user', {
attributes ....
}, {
hooks: {
afterCreate(user, options) {
user.testFunction();
}
}
});
// Instance methods
User.prototype.testFunction = () => {
this.firstName = "John";
}
// Class methods
User.anotherTestFunction = () => {
User.findOne().then(() => doSomething());
}
return User;
}
index.js
var sequelize;
sequelize = new Sequelize(config.DATABASE_URL);
db.User = sequelize.import(__dirname + '/user.js');
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
usersController.js
var db = require('../path/to/db');
function create_post_function = (req, res) => {
var body = getBody();
db.User.create(body).then(user => respondSuccess());
}
Now everything in this example works perfectly with the EXCEPT instance method !!!
I keep getting TypeError: Cannot set property 'firstName' of undefined
For some reason, it does not apply the instance method to the sequelize model. Very strange, but I'm probably doing something noticeably wrong and can't see it.
Really appreciate any help!
source
share