在尝试将Sequelize JS v4与ES6类一起使用时,我在执行实例方法时遇到了麻烦。由于某种原因,尽管它们是在代码中定义的,但它们似乎不存在。
这是一个例子-
模型文件
'use strict';
const Sequelize = require("sequelize");
class Model extends Sequelize.Model {
static init(sequelize, DataTypes) {
return super.init(
{
// properties
},
{ sequelize }
);
}
static associate(models) {
}
async modelMethod() {
}
}
module.exports = Model;
模型初始化
let modelClass = require('../models/' + modelFile);
let model = modelClass.init(sequelize, Sequelize);
然后将模型作为控制器的属性在控制器文件中调用
async controllerMethod(req, res) {
let info = await this.model.modelMethod();
res.send(info);
}
还有我收到的错误-
TypeError:this.model.modelMethod不是位于的函数 Controller.controllerMethod(/usr/app/controllers/controller.js:83:41)位于 Layer.handle [作为handle_request] (/usr/app/node_modules/express/lib/router/layer.js:95:5)在下一个 (/usr/app/node_modules/express/lib/router/route.js:137:13)在 路线派遣 (/usr/app/node_modules/express/lib/router/route.js:112:3)在 Layer.handle [作为handle_request] (/usr/app/node_modules/express/lib/router/layer.js:95:5)在 /usr/app/node_modules/express/lib/router/index.js:281:22 at param (/usr/app/node_modules/express/lib/router/index.js:354:14)在param (/usr/app/node_modules/express/lib/router/index.js:365:14)在param (/usr/app/node_modules/express/lib/router/index.js:365:14)在 Function.process_params (/usr/app/node_modules/express/lib/router/index.js:410:3)在下一个 (/usr/app/node_modules/express/lib/router/index.js:275:10)在 sequelize.models.Session.findOne.then(/usr/app/app.js:44:24)在 tryCatcher(/usr/app/node_modules/bluebird/js/release/util.js:16:23) 在Promise._settlePromiseFromHandler (/usr/app/node_modules/bluebird/js/release/promise.js:512:31)在 Promise._settle承诺 (/usr/app/node_modules/bluebird/js/release/promise.js:569:18)在 Promise._settlePromise0 (/usr/app/node_modules/bluebird/js/release/promise.js:614:10)在 Promise._settlePromise (/usr/app/node_modules/bluebird/js/release/promise.js:693:18)在 Async._drainQueue (/usr/app/node_modules/bluebird/js/release/async.js:133:16)在 Async._drainQueues (/usr/app/node_modules/bluebird/js/release/async.js:143:10)在 Instant.Async.drainQueues (/usr/app/node_modules/bluebird/js/release/async.js:17:14)在 立即参数(匿名函数)[为_onImmediate] (/usr/local/lib/node_modules/pm2/node_modules/event-loop-inspector/index.js:133:29) 在runCallback(timers.js:810:20)
尝试输出类的方法会得到-
console.log(Object.getOwnPropertyNames(this.model));
[ 'length',
'prototype',
'init',
'associate',
'name',
'sequelize',
'options',
'associations',
'underscored',
'tableName',
'_schema',
'_schemaDelimiter',
'rawAttributes',
'primaryKeys',
'_timestampAttributes',
'_readOnlyAttributes',
'_hasReadOnlyAttributes',
'_isReadOnlyAttribute',
'_dataTypeChanges',
'_dataTypeSanitizers',
'_booleanAttributes',
'_dateAttributes',
'_hstoreAttributes',
'_rangeAttributes',
'_jsonAttributes',
'_geometryAttributes',
'_virtualAttributes',
'_defaultValues',
'fieldRawAttributesMap',
'fieldAttributeMap',
'uniqueKeys',
'_hasBooleanAttributes',
'_isBooleanAttribute',
'_hasDateAttributes',
'_isDateAttribute',
'_hasHstoreAttributes',
'_isHstoreAttribute',
'_hasRangeAttributes',
'_isRangeAttribute',
'_hasJsonAttributes',
'_isJsonAttribute',
'_hasVirtualAttributes',
'_isVirtualAttribute',
'_hasGeometryAttributes',
'_isGeometryAttribute',
'_hasDefaultValues',
'attributes',
'tableAttributes',
'primaryKeyAttributes',
'primaryKeyAttribute',
'primaryKeyField',
'_hasPrimaryKeys',
'_isPrimaryKey',
'autoIncrementAttribute',
'_scope',
'_scopeNames' ]
答案 0 :(得分:1)
阅读Manish的评论后,我决定尝试一下。
显然,实例方法的工作方式是通过在调用.init方法之后初始化模型。
所以-
let model = ModelClass.init(sequelize, Sequelize);
model = new model();
这将允许没有问题地调用model.modelMethod
答案 1 :(得分:1)
static init
函数不是构造函数。它通过设置其功能来“初始化”该类,然后返回然后返回该类本身。
下面是一些示例代码来说明这一点:
class User extends Sequelize.Model {
static init(sequelize, DataTypes) {
return super.init({
firstName: {
type: DataTypes.STRING
},
lastName: {
type: DataTypes.STRING
}
}, {
sequelize
});
}
fullname() {
return `${this.firstName} ${this.lastName}`;
}
}
// You don't need to capture the return here, I'm just doing it to show what it is.
const result = User.init(sequelize, Sequelize);
console.log(result === User); // true
const user = new User();
console.log(typeof user.fullname === 'function'); // true