1
votes

Defined my User.js model as under but getting below exception

Error

PS C:\Users\meghshyam\Documents\express_node_mysql_handlebars> node server.js C:\Users\meghshyam\Documents\express_node_mysql_handlebars\node_modules\sequelize\lib\sequelize.js:691 this.importCache[path] = defineCall(this, DataTypes); ^

TypeError: defineCall is not a function at Sequelize.import (C:\Users\meghshyam\Documents\express_node_mysql_handlebars\node_modules\sequelize\lib\sequelize.js:691:30) at C:\Users\meghshyam\Documents\express_node_mysql_handlebars\models\index.js:23:36 at Array.forEach (native) at Object. (C:\Users\meghshyam\Documents\express_node_mysql_handlebars\models\index.js:22:4) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Object. (C:\Users\meghshyam\Documents\express_node_mysql_handlebars\server.js:16:10) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Function.Module.runMain (module.js:441:10)

 "use strict";

 module.exports = function(sequelize, DataTypes) {
 var User = sequelize.define("User", 
 {
 //timestamps: true,
 // id:DataTypes.INTEGER,
  username:{ 
  type:DataTypes.STRING,
  validate:{
    isEmail:true
  }
},
password: {
  type: DataTypes.STRING,
  validate:{
    len:[6,12],
    notNull:true,
    notEmpty:true
  }
},
 role:{
  type: DataTypes.INTEGER,
  validate:{
    notNull:true,
    notEmpty:true,
    isNumeric:true
  },
  }
 },

  {
 classMethods: {
 associate: function(models) {
  // Associating Author with Posts
  // When an Author is deleted, also delete any associated Posts
 // User.belongsTo(models.Student, {
 //   onDelete: "cascade"
 // });
//user has one teacher record
//   User.belongsTo(models.Teacher, {
//    onDelete: "cascade"
  });
  }
  }
  }
  );
return User;
 };
2

2 Answers

0
votes

This is normally caused by importing files that aren't Sequelize models in your index.js. Do you have anything in your models directory that isn't a model? In my case, it was because it was pulling in my test files as well as the models, which were in the same directory. Try adding another check into index.js where it builds the file list to exclude anything that ends with .test.js (or whatever the equivalent is for you).

0
votes

I've been having the same problem, I didn't understant your method but for me worked this:

var CurrencyShop = require('./models/CurrencyShop.js')(sequelize, Sequelize.DataTypes);
require('./models/Users.js')(sequelize, Sequelize.DataTypes);
require('./models/UserItems.js')(sequelize, Sequelize.DataTypes);