热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

排序Model.prototype。(customFunction)不起作用

如何解决《排序Model.prototype。(customFunction)不起作用》经验,为你挑选了1个好方法。

我试图在Sequelize模型中定义一个customFunction 。但是我得到一个错误:

TypeError:user.getJWT不是User.create.then的函数(/projects/test/a/app/controllers/UserController.js:22:29)

这是以下代码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
    }

这就是我在控制器中调用此原型函数的方式...

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);
    }

我也尝试使用,User.Instance.prototype.getJWT但是由于我正在使用sequelize 4.xx版,所以甚至无法使用

这是user对象的日志:

{ [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/company.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' ] }

PhilippeAuri.. 6

您的函数“定义”未正确关闭。根据此处的文档,以下内容应起作用。

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
    }

看到user对象的日志,它看起来不像是User模型的适当实例(而是像模型本身)。您能显示控制器本身,特别是在您需要模型的需求阶段吗?你可以尝试这样的控制器:

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);
}

这不应正确输出用户:



1> PhilippeAuri..:

您的函数“定义”未正确关闭。根据此处的文档,以下内容应起作用。

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
    }

看到user对象的日志,它看起来不像是User模型的适当实例(而是像模型本身)。您能显示控制器本身,特别是在您需要模型的需求阶段吗?你可以尝试这样的控制器:

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);
}

这不应正确输出用户:


推荐阅读
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • 本文介绍了如何在Mac上使用Pillow库加载不同于默认字体和大小的字体,并提供了一个简单的示例代码。通过该示例,读者可以了解如何在Python中使用Pillow库来写入不同字体的文本。同时,本文也解决了在Mac上使用Pillow库加载字体时可能遇到的问题。读者可以根据本文提供的示例代码,轻松实现在Mac上使用Pillow库加载不同字体的功能。 ... [详细]
  • 本文介绍了在使用Laravel和sqlsrv连接到SQL Server 2016时,如何在插入查询中使用输出子句,并返回所需的值。同时讨论了使用CreatedOn字段返回最近创建的行的解决方法以及使用Eloquent模型创建后,值正确插入数据库但没有返回uniqueidentifier字段的问题。最后给出了一个示例代码。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 导出功能protectedvoidbtnExport(objectsender,EventArgse){用来打开下载窗口stringfileName中 ... [详细]
  • React项目中运用React技巧解决实际问题的总结
    本文总结了在React项目中如何运用React技巧解决一些实际问题,包括取消请求和页面卸载的关联,利用useEffect和AbortController等技术实现请求的取消。文章中的代码是简化后的例子,但思想是相通的。 ... [详细]
  • 本文介绍了在wepy中运用小顺序页面受权的计划,包含了用户点击作废后的从新受权计划。 ... [详细]
  • MPLS VP恩 后门链路shamlink实验及配置步骤
    本文介绍了MPLS VP恩 后门链路shamlink的实验步骤及配置过程,包括拓扑、CE1、PE1、P1、P2、PE2和CE2的配置。详细讲解了shamlink实验的目的和操作步骤,帮助读者理解和实践该技术。 ... [详细]
  • 本文介绍了如何使用elementui分页组件进行分页功能的改写,只需一行代码即可调用。通过封装分页组件,避免在每个页面都写跳转请求的重复代码。详细的代码示例和使用方法在正文中给出。 ... [详细]
  • 如何用JNI技术调用Java接口以及提高Java性能的详解
    本文介绍了如何使用JNI技术调用Java接口,并详细解析了如何通过JNI技术提高Java的性能。同时还讨论了JNI调用Java的private方法、Java开发中使用JNI技术的情况以及使用Java的JNI技术调用C++时的运行效率问题。文章还介绍了JNIEnv类型的使用方法,包括创建Java对象、调用Java对象的方法、获取Java对象的属性等操作。 ... [详细]
  • 本文介绍了在满足特定条件时如何在输入字段中使用默认值的方法和相应的代码。当输入字段填充100或更多的金额时,使用50作为默认值;当输入字段填充有-20或更多(负数)时,使用-10作为默认值。文章还提供了相关的JavaScript和Jquery代码,用于动态地根据条件使用默认值。 ... [详细]
  • 前段时间做一个项目,需求是对每个视频添加预览图,这个问题最终选择方案是:用canvas.toDataYRL();来做转换获取视频的一个截图,添加到页面中,达到自动添加预览图的目的。 ... [详细]
  • PatchODAX8: ... [详细]
author-avatar
涛之圣首到
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有