WhyAyala WhyAyala - 5 months ago 50
Node.js Question

Sequelize says instanceMethod is not defined

I'm using sequelize to connect to a mysql db for development. I have a model called Dealer:

'use strict';
module.exports = function(sequelize, DataTypes) {
var Dealer = sequelize.define('Dealer', {
id: { allowNull: false, autoIncrement: true,
primaryKey: true, type: DataTypes.INTEGER.UNSIGNED },
...
created_at: { allowNull: false, type: DataTypes.DATE },
updated_at: { allowNull: false, type: DataTypes.DATE }
},
{underscored: true},
{
classMethods: {
associate: function(models) {
Dealer.hasMany(models.Job);
}
},
instanceMethods: {
getAllClientData: function(){
leads = [];
...
return leads;
},
}
});
return Dealer;
};


When I try to call the instance method on an object returned by a sequelize query in my dealerController.js file:

dealer.getAllClientData()


I get the error:

Unhandled rejection TypeError: dealer.getAllClientData is not a function


When i print the returned JSON to the console it reads as such:

{ dataValues:
{ id: 1,
....
}
...
'$modelOptions':
{ timestamps: true,
instanceMethods: {},
classMethods: {},
validate: {},
freezeTableName: false,
underscored: true,
underscoredAll: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: { id: '1' },
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: [],
hooks: {},
indexes: [],
name: { plural: 'Dealers', singular: 'Dealer' },
omitNul: false,
...
}
...
}


Obviously my instanceMethod is not defined, and according to the sequelize docs I should have getters and setters available too.

I don't understand what step i'm missing here as I've read through much of the sequelize docs and even used their cli to generate the models and migrations.

Any thoughts?

Edit:



Here is what is output to log for dealer.prototype

{ _customGetters: {},
_customSetters: {},
validators: {},
_hasCustomGetters: 0,
_hasCustomSetters: 0,
rawAttributes:
{ id:
{ allowNull: false,
autoIncrement: true,
primaryKey: true,
type: [Object],
Model: Dealer,
fieldName: 'id',
_modelAttribute: true,
field: 'id' },
///Other Attributes
},
_isAttribute: { [Function] cache: MapCache { __data__: [Object] } },
Model: Dealer,
$Model': Dealer }

Answer

After reading the docs a little further and looking at some other model definitions I discovered that the issue was I had defined my model incorrectly.

In my definition above you'll notice I wrapped the underscored: true option in brackets, followed by my classMethods and instanceMethods wrapped in another set of brackets.

This is incorrect. The proper way to define a sequelize model is with two sets of brackets, the first containing your model attributes and the second containing all other options, including methods.

'use strict';
module.exports = function(sequelize, DataTypes) {
  var Dealer = sequelize.define('Dealer', {
    id:         { allowNull: false, autoIncrement: true,
                  primaryKey: true, type: DataTypes.INTEGER.UNSIGNED },
    ...
    created_at: { allowNull: false, type: DataTypes.DATE },
    updated_at: { allowNull: false, type: DataTypes.DATE }
  },
  {
    underscored: true,
    classMethods: {
      associate: function(models) {
        Dealer.hasMany(models.Job);
      }
    },
    instanceMethods: {
      getAllClientData: function(){
        leads = [];
        ...
        return leads;
      },
    }
  });
  return Dealer;
};
Comments