admin管理员组

文章数量:1334286

I am trying to define a customFunction in Sequelize model. But I get an error :

TypeError: user.getJWT is not a function at User.create.then (/projects/test/a/app/controllers/UserController.js:22:29)

Here is the code of models/user.js:

module.exports = function(sequelize, Sequelize) {
    var User = sequelize.define('User', {
        id: {
          type: Sequelize.INTEGER(11),
          allowNull: true,
          primaryKey: true,
          autoIncrement: true
        },
        user_id: {
          type: Sequelize.STRING(255),
          allowNull: true,
          defaultValue: ''
        }
      });

      User.prototype.getJWT = function() {
        let expiration_time = parseInt(CONFIG.jwt_expiration);
        return "Bearer " + jwt.sign({
          user_id: this.user_id,
          role: this.role
        }, CONFIG.jwt_encryption, {
          expiresIn: expiration_time
        });
      }
      return User
    }

This is how I am calling this prototype function in my controller...

User.create(body).then((user) => {
      user.token = user.getJWT(); // and here I get the error ..TypeError: user.getJWT is not a function 
      res.json(user);
    }

I also tried using User.Instance.prototype.getJWT But that even does not work as I am using sequelize version 4.x.x

Here is the log of user object :

{ [Function: User]
  sequelize:
   Sequelize {
     options:
      { dialect: 'mysql',
        dialectModulePath: null,
        host: 'localhost',
        protocol: 'tcp',
        define: [Object],
        query: {},
        sync: {},
        timezone: '+00:00',
        logging: [Function: bound consoleCall],
        omitNull: false,
        native: false,
        replication: false,
        ssl: undefined,
        pool: {},
        quoteIdentifiers: true,
        hooks: {},
        retry: [Object],
        transactionType: 'DEFERRED',
        isolationLevel: null,
        databaseVersion: '5.6.0',
        typeValidation: false,
        benchmark: false,
        operatorsAliases: false,
        port: '3306' },
     config:
      { database: 'mlmapi',
        username: 'root',
        password: 'a5f7c674',
        host: 'localhost',
        port: '3306',
        pool: {},
        protocol: 'tcp',
        native: false,
        ssl: undefined,
        replication: false,
        dialectModulePath: null,
        keepDefaultTimezone: undefined,
        dialectOptions: undefined },
     dialect:
      MysqlDialect {
        sequelize: [Circular],
        connectionManager: [Object],
        QueryGenerator: [Object] },
     queryInterface: QueryInterface { sequelize: [Circular], QueryGenerator: [Object] },
     models:
      { Company: [Object],
        Dashboard: [Object],
        User: [Circular],
        UserCompany: [Object] },
     modelManager: ModelManager { models: [Array], sequelize: [Circular] },
     connectionManager:
      ConnectionManager {
        sequelize: [Circular],
        config: [Object],
        dialect: [Object],
        versionPromise: null,
        dialectName: 'mysql',
        pool: [Object],
        lib: [Object] },
     importCache:
      { '/projects/test/code/models/pany.model.js': [Object],
        '/projects/test/code/models/dashboard.js': [Object],
        '/projects/test/code/models/user.model.js': [Circular] },
     test:
      { _trackRunningQueries: false,
        _runningQueries: 0,
        trackRunningQueries: [Function: trackRunningQueries],
        verifyNoRunningQueries: [Function: verifyNoRunningQueries] } },
  options:
   { timestamps: false,
     validate: {},
     freezeTableName: false,
     underscored: false,
     underscoredAll: false,
     paranoid: false,
     rejectOnEmpty: false,
     whereCollection: { id: 3 },
     schema: null,
     schemaDelimiter: '',
     defaultScope: {},
     scopes: [],
     indexes: [],
     name: { plural: 'Users', singular: 'User' },
     omitNull: false,
     tableName: 'user',
     sequelize:
      Sequelize {
        options: [Object],
        config: [Object],
        dialect: [Object],
        queryInterface: [Object],
        models: [Object],
        modelManager: [Object],
        connectionManager: [Object],
        importCache: [Object],
        test: [Object] },
     hooks:
      { beforeUpdate: [Array],
        beforeCreate: [Array],
        beforeSave: [Array] },
     uniqueKeys: {} },
  associations: {},
  underscored: undefined,
  tableName: 'user',
  _schema: null,
  _schemaDelimiter: '',
  rawAttributes:
   { id:
      { type: [Object],
        allowNull: true,
        primaryKey: true,
        autoIncrement: true,
        Model: [Circular],
        fieldName: 'id',
        _modelAttribute: true,
        field: 'id' },
     name:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'name',
        _modelAttribute: true,
        field: 'name' },
     user_id:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'user_id',
        _modelAttribute: true,
        field: 'user_id' },
     role:
      { type: [Object],
        allowNull: true,
        defaultValue: 'user',
        Model: [Circular],
        fieldName: 'role',
        _modelAttribute: true,
        field: 'role' },
     email:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'email',
        _modelAttribute: true,
        field: 'email' },
     password:
      { type: [Object],
        allowNull: true,
        Model: [Circular],
        fieldName: 'password',
        _modelAttribute: true,
        field: 'password' },
     position:
      { type: [Object],
        allowNull: true,
        Model: [Circular],
        fieldName: 'position',
        _modelAttribute: true,
        field: 'position' },
     sponsor:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'sponsor',
        _modelAttribute: true,
        field: 'sponsor' },
     master_sponsor:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'master_sponsor',
        _modelAttribute: true,
        field: 'master_sponsor' },
     master_key:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'master_key',
        _modelAttribute: true,
        field: 'master_key' },
     packageAmount:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'packageAmount',
        _modelAttribute: true,
        field: 'packageAmount' },
     status:
      { type: [Object],
        allowNull: true,
        defaultValue: '1',
        Model: [Circular],
        fieldName: 'status',
        _modelAttribute: true,
        field: 'status' },
     main_id:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'main_id',
        _modelAttribute: true,
        field: 'main_id' },
     upline:
      { type: [Object],
        allowNull: true,
        defaultValue: '{level:}',
        Model: [Circular],
        fieldName: 'upline',
        _modelAttribute: true,
        field: 'upline' } },
  primaryKeys:
   { id:
      { type: [Object],
        allowNull: true,
        primaryKey: true,
        autoIncrement: true,
        Model: [Circular],
        fieldName: 'id',
        _modelAttribute: true,
        field: 'id' } },
  _timestampAttributes: {},
  _readOnlyAttributes: [],
  _hasReadOnlyAttributes: 0,
  _isReadOnlyAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _dataTypeChanges: {},
  _dataTypeSanitizers: {},
  _booleanAttributes: [],
  _dateAttributes: [],
  _hstoreAttributes: [],
  _rangeAttributes: [],
  _jsonAttributes: [],
  _geometryAttributes: [],
  _virtualAttributes: [],
  _defaultValues:
   { name: [Function: wrapper],
     user_id: [Function: wrapper],
     role: [Function: wrapper],
     email: [Function: wrapper],
     sponsor: [Function: wrapper],
     master_sponsor: [Function: wrapper],
     master_key: [Function: wrapper],
     packageAmount: [Function: wrapper],
     status: [Function: wrapper],
     main_id: [Function: wrapper],
     upline: [Function: wrapper] },
  fieldRawAttributesMap:
   { id:
      { type: [Object],
        allowNull: true,
        primaryKey: true,
        autoIncrement: true,
        Model: [Circular],
        fieldName: 'id',
        _modelAttribute: true,
        field: 'id' },
     name:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'name',
        _modelAttribute: true,
        field: 'name' },
     user_id:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'user_id',
        _modelAttribute: true,
        field: 'user_id' },
     role:
      { type: [Object],
        allowNull: true,
        defaultValue: 'user',
        Model: [Circular],
        fieldName: 'role',
        _modelAttribute: true,
        field: 'role' },
     email:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'email',
        _modelAttribute: true,
        field: 'email' },
     password:
      { type: [Object],
        allowNull: true,
        Model: [Circular],
        fieldName: 'password',
        _modelAttribute: true,
        field: 'password' },
     position:
      { type: [Object],
        allowNull: true,
        Model: [Circular],
        fieldName: 'position',
        _modelAttribute: true,
        field: 'position' },
     sponsor:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'sponsor',
        _modelAttribute: true,
        field: 'sponsor' },
     master_sponsor:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'master_sponsor',
        _modelAttribute: true,
        field: 'master_sponsor' },
     master_key:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'master_key',
        _modelAttribute: true,
        field: 'master_key' },
     packageAmount:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'packageAmount',
        _modelAttribute: true,
        field: 'packageAmount' },
     status:
      { type: [Object],
        allowNull: true,
        defaultValue: '1',
        Model: [Circular],
        fieldName: 'status',
        _modelAttribute: true,
        field: 'status' },
     main_id:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'main_id',
        _modelAttribute: true,
        field: 'main_id' },
     upline:
      { type: [Object],
        allowNull: true,
        defaultValue: '{level:}',
        Model: [Circular],
        fieldName: 'upline',
        _modelAttribute: true,
        field: 'upline' } },
  fieldAttributeMap: {},
  uniqueKeys: {},
  _hasBooleanAttributes: false,
  _isBooleanAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _hasDateAttributes: false,
  _isDateAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _hasHstoreAttributes: false,
  _isHstoreAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _hasRangeAttributes: false,
  _isRangeAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _hasJsonAttributes: false,
  _isJsonAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _hasVirtualAttributes: false,
  _isVirtualAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _hasGeometryAttributes: false,
  _isGeometryAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _hasDefaultValues: true,
  attributes:
   { id:
      { type: [Object],
        allowNull: true,
        primaryKey: true,
        autoIncrement: true,
        Model: [Circular],
        fieldName: 'id',
        _modelAttribute: true,
        field: 'id' },
     ....
     .....
     upline:
      { type: [Object],
        allowNull: true,
        defaultValue: '{level:}',
        Model: [Circular],
        fieldName: 'upline',
        _modelAttribute: true,
        field: 'upline' } },
  tableAttributes:
   { id:
      { type: [Object],
        allowNull: true,
        primaryKey: true,
        autoIncrement: true,
        Model: [Circular],
        fieldName: 'id',
        _modelAttribute: true,
        field: 'id' },
     name:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'name',
        _modelAttribute: true,
        field: 'name' },
     user_id:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'user_id',
        _modelAttribute: true,
        field: 'user_id' },
     role:
      { type: [Object],
        allowNull: true,
        defaultValue: 'user',
        Model: [Circular],
        fieldName: 'role',
        _modelAttribute: true,
        field: 'role' },
     email:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'email',
        _modelAttribute: true,
        field: 'email' },
     password:
      { type: [Object],
        allowNull: true,
        Model: [Circular],
        fieldName: 'password',
        _modelAttribute: true,
        field: 'password' },
     position:
      { type: [Object],
        allowNull: true,
        Model: [Circular],
        fieldName: 'position',
        _modelAttribute: true,
        field: 'position' },
     sponsor:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'sponsor',
        _modelAttribute: true,
        field: 'sponsor' },
     master_sponsor:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'master_sponsor',
        _modelAttribute: true,
        field: 'master_sponsor' },
     master_key:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'master_key',
        _modelAttribute: true,
        field: 'master_key' },
     packageAmount:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'packageAmount',
        _modelAttribute: true,
        field: 'packageAmount' },
     status:
      { type: [Object],
        allowNull: true,
        defaultValue: '1',
        Model: [Circular],
        fieldName: 'status',
        _modelAttribute: true,
        field: 'status' },
     main_id:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'main_id',
        _modelAttribute: true,
        field: 'main_id' },
     upline:
      { type: [Object],
        allowNull: true,
        defaultValue: '{level:}',
        Model: [Circular],
        fieldName: 'upline',
        _modelAttribute: true,
        field: 'upline' } },
  primaryKeyAttributes: [ 'id' ],
  primaryKeyAttribute: 'id',
  primaryKeyField: 'id',
  _hasPrimaryKeys: true,
  _isPrimaryKey: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  autoIncrementAttribute: 'id',
  _scope: {},
  _scopeNames: [ 'defaultScope' ] }

I am trying to define a customFunction in Sequelize model. But I get an error :

TypeError: user.getJWT is not a function at User.create.then (/projects/test/a/app/controllers/UserController.js:22:29)

Here is the code of models/user.js:

module.exports = function(sequelize, Sequelize) {
    var User = sequelize.define('User', {
        id: {
          type: Sequelize.INTEGER(11),
          allowNull: true,
          primaryKey: true,
          autoIncrement: true
        },
        user_id: {
          type: Sequelize.STRING(255),
          allowNull: true,
          defaultValue: ''
        }
      });

      User.prototype.getJWT = function() {
        let expiration_time = parseInt(CONFIG.jwt_expiration);
        return "Bearer " + jwt.sign({
          user_id: this.user_id,
          role: this.role
        }, CONFIG.jwt_encryption, {
          expiresIn: expiration_time
        });
      }
      return User
    }

This is how I am calling this prototype function in my controller...

User.create(body).then((user) => {
      user.token = user.getJWT(); // and here I get the error ..TypeError: user.getJWT is not a function 
      res.json(user);
    }

I also tried using User.Instance.prototype.getJWT But that even does not work as I am using sequelize version 4.x.x

Here is the log of user object :

{ [Function: User]
  sequelize:
   Sequelize {
     options:
      { dialect: 'mysql',
        dialectModulePath: null,
        host: 'localhost',
        protocol: 'tcp',
        define: [Object],
        query: {},
        sync: {},
        timezone: '+00:00',
        logging: [Function: bound consoleCall],
        omitNull: false,
        native: false,
        replication: false,
        ssl: undefined,
        pool: {},
        quoteIdentifiers: true,
        hooks: {},
        retry: [Object],
        transactionType: 'DEFERRED',
        isolationLevel: null,
        databaseVersion: '5.6.0',
        typeValidation: false,
        benchmark: false,
        operatorsAliases: false,
        port: '3306' },
     config:
      { database: 'mlmapi',
        username: 'root',
        password: 'a5f7c674',
        host: 'localhost',
        port: '3306',
        pool: {},
        protocol: 'tcp',
        native: false,
        ssl: undefined,
        replication: false,
        dialectModulePath: null,
        keepDefaultTimezone: undefined,
        dialectOptions: undefined },
     dialect:
      MysqlDialect {
        sequelize: [Circular],
        connectionManager: [Object],
        QueryGenerator: [Object] },
     queryInterface: QueryInterface { sequelize: [Circular], QueryGenerator: [Object] },
     models:
      { Company: [Object],
        Dashboard: [Object],
        User: [Circular],
        UserCompany: [Object] },
     modelManager: ModelManager { models: [Array], sequelize: [Circular] },
     connectionManager:
      ConnectionManager {
        sequelize: [Circular],
        config: [Object],
        dialect: [Object],
        versionPromise: null,
        dialectName: 'mysql',
        pool: [Object],
        lib: [Object] },
     importCache:
      { '/projects/test/code/models/pany.model.js': [Object],
        '/projects/test/code/models/dashboard.js': [Object],
        '/projects/test/code/models/user.model.js': [Circular] },
     test:
      { _trackRunningQueries: false,
        _runningQueries: 0,
        trackRunningQueries: [Function: trackRunningQueries],
        verifyNoRunningQueries: [Function: verifyNoRunningQueries] } },
  options:
   { timestamps: false,
     validate: {},
     freezeTableName: false,
     underscored: false,
     underscoredAll: false,
     paranoid: false,
     rejectOnEmpty: false,
     whereCollection: { id: 3 },
     schema: null,
     schemaDelimiter: '',
     defaultScope: {},
     scopes: [],
     indexes: [],
     name: { plural: 'Users', singular: 'User' },
     omitNull: false,
     tableName: 'user',
     sequelize:
      Sequelize {
        options: [Object],
        config: [Object],
        dialect: [Object],
        queryInterface: [Object],
        models: [Object],
        modelManager: [Object],
        connectionManager: [Object],
        importCache: [Object],
        test: [Object] },
     hooks:
      { beforeUpdate: [Array],
        beforeCreate: [Array],
        beforeSave: [Array] },
     uniqueKeys: {} },
  associations: {},
  underscored: undefined,
  tableName: 'user',
  _schema: null,
  _schemaDelimiter: '',
  rawAttributes:
   { id:
      { type: [Object],
        allowNull: true,
        primaryKey: true,
        autoIncrement: true,
        Model: [Circular],
        fieldName: 'id',
        _modelAttribute: true,
        field: 'id' },
     name:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'name',
        _modelAttribute: true,
        field: 'name' },
     user_id:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'user_id',
        _modelAttribute: true,
        field: 'user_id' },
     role:
      { type: [Object],
        allowNull: true,
        defaultValue: 'user',
        Model: [Circular],
        fieldName: 'role',
        _modelAttribute: true,
        field: 'role' },
     email:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'email',
        _modelAttribute: true,
        field: 'email' },
     password:
      { type: [Object],
        allowNull: true,
        Model: [Circular],
        fieldName: 'password',
        _modelAttribute: true,
        field: 'password' },
     position:
      { type: [Object],
        allowNull: true,
        Model: [Circular],
        fieldName: 'position',
        _modelAttribute: true,
        field: 'position' },
     sponsor:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'sponsor',
        _modelAttribute: true,
        field: 'sponsor' },
     master_sponsor:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'master_sponsor',
        _modelAttribute: true,
        field: 'master_sponsor' },
     master_key:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'master_key',
        _modelAttribute: true,
        field: 'master_key' },
     packageAmount:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'packageAmount',
        _modelAttribute: true,
        field: 'packageAmount' },
     status:
      { type: [Object],
        allowNull: true,
        defaultValue: '1',
        Model: [Circular],
        fieldName: 'status',
        _modelAttribute: true,
        field: 'status' },
     main_id:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'main_id',
        _modelAttribute: true,
        field: 'main_id' },
     upline:
      { type: [Object],
        allowNull: true,
        defaultValue: '{level:}',
        Model: [Circular],
        fieldName: 'upline',
        _modelAttribute: true,
        field: 'upline' } },
  primaryKeys:
   { id:
      { type: [Object],
        allowNull: true,
        primaryKey: true,
        autoIncrement: true,
        Model: [Circular],
        fieldName: 'id',
        _modelAttribute: true,
        field: 'id' } },
  _timestampAttributes: {},
  _readOnlyAttributes: [],
  _hasReadOnlyAttributes: 0,
  _isReadOnlyAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _dataTypeChanges: {},
  _dataTypeSanitizers: {},
  _booleanAttributes: [],
  _dateAttributes: [],
  _hstoreAttributes: [],
  _rangeAttributes: [],
  _jsonAttributes: [],
  _geometryAttributes: [],
  _virtualAttributes: [],
  _defaultValues:
   { name: [Function: wrapper],
     user_id: [Function: wrapper],
     role: [Function: wrapper],
     email: [Function: wrapper],
     sponsor: [Function: wrapper],
     master_sponsor: [Function: wrapper],
     master_key: [Function: wrapper],
     packageAmount: [Function: wrapper],
     status: [Function: wrapper],
     main_id: [Function: wrapper],
     upline: [Function: wrapper] },
  fieldRawAttributesMap:
   { id:
      { type: [Object],
        allowNull: true,
        primaryKey: true,
        autoIncrement: true,
        Model: [Circular],
        fieldName: 'id',
        _modelAttribute: true,
        field: 'id' },
     name:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'name',
        _modelAttribute: true,
        field: 'name' },
     user_id:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'user_id',
        _modelAttribute: true,
        field: 'user_id' },
     role:
      { type: [Object],
        allowNull: true,
        defaultValue: 'user',
        Model: [Circular],
        fieldName: 'role',
        _modelAttribute: true,
        field: 'role' },
     email:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'email',
        _modelAttribute: true,
        field: 'email' },
     password:
      { type: [Object],
        allowNull: true,
        Model: [Circular],
        fieldName: 'password',
        _modelAttribute: true,
        field: 'password' },
     position:
      { type: [Object],
        allowNull: true,
        Model: [Circular],
        fieldName: 'position',
        _modelAttribute: true,
        field: 'position' },
     sponsor:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'sponsor',
        _modelAttribute: true,
        field: 'sponsor' },
     master_sponsor:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'master_sponsor',
        _modelAttribute: true,
        field: 'master_sponsor' },
     master_key:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'master_key',
        _modelAttribute: true,
        field: 'master_key' },
     packageAmount:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'packageAmount',
        _modelAttribute: true,
        field: 'packageAmount' },
     status:
      { type: [Object],
        allowNull: true,
        defaultValue: '1',
        Model: [Circular],
        fieldName: 'status',
        _modelAttribute: true,
        field: 'status' },
     main_id:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'main_id',
        _modelAttribute: true,
        field: 'main_id' },
     upline:
      { type: [Object],
        allowNull: true,
        defaultValue: '{level:}',
        Model: [Circular],
        fieldName: 'upline',
        _modelAttribute: true,
        field: 'upline' } },
  fieldAttributeMap: {},
  uniqueKeys: {},
  _hasBooleanAttributes: false,
  _isBooleanAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _hasDateAttributes: false,
  _isDateAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _hasHstoreAttributes: false,
  _isHstoreAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _hasRangeAttributes: false,
  _isRangeAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _hasJsonAttributes: false,
  _isJsonAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _hasVirtualAttributes: false,
  _isVirtualAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _hasGeometryAttributes: false,
  _isGeometryAttribute: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  _hasDefaultValues: true,
  attributes:
   { id:
      { type: [Object],
        allowNull: true,
        primaryKey: true,
        autoIncrement: true,
        Model: [Circular],
        fieldName: 'id',
        _modelAttribute: true,
        field: 'id' },
     ....
     .....
     upline:
      { type: [Object],
        allowNull: true,
        defaultValue: '{level:}',
        Model: [Circular],
        fieldName: 'upline',
        _modelAttribute: true,
        field: 'upline' } },
  tableAttributes:
   { id:
      { type: [Object],
        allowNull: true,
        primaryKey: true,
        autoIncrement: true,
        Model: [Circular],
        fieldName: 'id',
        _modelAttribute: true,
        field: 'id' },
     name:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'name',
        _modelAttribute: true,
        field: 'name' },
     user_id:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'user_id',
        _modelAttribute: true,
        field: 'user_id' },
     role:
      { type: [Object],
        allowNull: true,
        defaultValue: 'user',
        Model: [Circular],
        fieldName: 'role',
        _modelAttribute: true,
        field: 'role' },
     email:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'email',
        _modelAttribute: true,
        field: 'email' },
     password:
      { type: [Object],
        allowNull: true,
        Model: [Circular],
        fieldName: 'password',
        _modelAttribute: true,
        field: 'password' },
     position:
      { type: [Object],
        allowNull: true,
        Model: [Circular],
        fieldName: 'position',
        _modelAttribute: true,
        field: 'position' },
     sponsor:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'sponsor',
        _modelAttribute: true,
        field: 'sponsor' },
     master_sponsor:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'master_sponsor',
        _modelAttribute: true,
        field: 'master_sponsor' },
     master_key:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'master_key',
        _modelAttribute: true,
        field: 'master_key' },
     packageAmount:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'packageAmount',
        _modelAttribute: true,
        field: 'packageAmount' },
     status:
      { type: [Object],
        allowNull: true,
        defaultValue: '1',
        Model: [Circular],
        fieldName: 'status',
        _modelAttribute: true,
        field: 'status' },
     main_id:
      { type: [Object],
        allowNull: true,
        defaultValue: '',
        Model: [Circular],
        fieldName: 'main_id',
        _modelAttribute: true,
        field: 'main_id' },
     upline:
      { type: [Object],
        allowNull: true,
        defaultValue: '{level:}',
        Model: [Circular],
        fieldName: 'upline',
        _modelAttribute: true,
        field: 'upline' } },
  primaryKeyAttributes: [ 'id' ],
  primaryKeyAttribute: 'id',
  primaryKeyField: 'id',
  _hasPrimaryKeys: true,
  _isPrimaryKey: { [Function: memoized] cache: MapCache { size: 0, __data__: [Object] } },
  autoIncrementAttribute: 'id',
  _scope: {},
  _scopeNames: [ 'defaultScope' ] }
Share Improve this question edited Jul 22, 2018 at 14:06 Ramesh Pareek asked Jul 17, 2018 at 13:23 Ramesh PareekRamesh Pareek 1,6694 gold badges31 silver badges55 bronze badges 1
  • Please answer and I will send you some btc – Ramesh Pareek Commented Jul 17, 2018 at 20:25
Add a ment  | 

2 Answers 2

Reset to default 6 +100

Your function 'define' is not closed correctly. According to the documentation here the following should work.

module.exports = function(sequelize, Sequelize) {
    var User = sequelize.define('User', {
        id: {
          type: Sequelize.INTEGER(11),
          allowNull: true,
          primaryKey: true,
          autoIncrement: true
        },
        user_id: {
          type: Sequelize.STRING(255),
          allowNull: true,
          defaultValue: ''
        }
      }); // you missed a closing parenthesis here

      User.prototype.getJWT = function() {
        let expiration_time = parseInt(CONFIG.jwt_expiration);
        return "Bearer " + jwt.sign({
          user_id: this.user_id,
          role: this.role
        }, CONFIG.jwt_encryption, {
          expiresIn: expiration_time
        });
      }
      return User
    }

Seeing the log of the user object it does not look like a proper instance of the User model (but rather like a model itself). Could you show the controller itself, especially the require phase where you require your models ? And could you try the controller like this :

User.create(body).then((user) => {
    user.token = user.getJWT(); // and here I get the error ..TypeError: user.getJWT is not a function 
    res.json(user);
}

This should not output the user correctly :s

Have you tried using it like this:

    module.exports = function (sequelize, Sequelize) {
        // ... model

        User.getJWT = function () {
            let expiration_time = parseInt(CONFIG.jwt_expiration);
            return "Bearer " + jwt.sign({
                user_id: this.user_id,
                role: this.role
            }, CONFIG.jwt_encryption, {
                    expiresIn: expiration_time
                });
        }
        return User
    }

You are specifying an instance level method, and you are trying to use a class level method. (Check the Expansion of models section)

本文标签: javascriptSequelize Modelprototype(customFunction) not workingStack Overflow