gpt4 book ai didi

javascript - 继承不存在的 ES6 模型方法

转载 作者:行者123 更新时间:2023-11-29 17:41:27 26 4
gpt4 key购买 nike

在尝试将 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);

然后模型作为 Controller 的属性在 Controller 文件中被调用

async controllerMethod(req, res) {
let info = await this.model.modelMethod();
res.send(info);
}

以及我收到的错误 -

TypeError: this.model.modelMethod is not a function at Controller.controllerMethod (/usr/app/controllers/controller.js:83:41) at Layer.handle [as handle_request] (/usr/app/node_modules/express/lib/router/layer.js:95:5) at next (/usr/app/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/usr/app/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/usr/app/node_modules/express/lib/router/layer.js:95:5) at /usr/app/node_modules/express/lib/router/index.js:281:22 at param (/usr/app/node_modules/express/lib/router/index.js:354:14) at param (/usr/app/node_modules/express/lib/router/index.js:365:14) at param (/usr/app/node_modules/express/lib/router/index.js:365:14) at Function.process_params (/usr/app/node_modules/express/lib/router/index.js:410:3) at next (/usr/app/node_modules/express/lib/router/index.js:275:10) at sequelize.models.Session.findOne.then (/usr/app/app.js:44:24) at tryCatcher (/usr/app/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/usr/app/node_modules/bluebird/js/release/promise.js:512:31) at Promise._settlePromise (/usr/app/node_modules/bluebird/js/release/promise.js:569:18) at Promise._settlePromise0 (/usr/app/node_modules/bluebird/js/release/promise.js:614:10) at Promise._settlePromises (/usr/app/node_modules/bluebird/js/release/promise.js:693:18) at Async._drainQueue (/usr/app/node_modules/bluebird/js/release/async.js:133:16) at Async._drainQueues (/usr/app/node_modules/bluebird/js/release/async.js:143:10) at Immediate.Async.drainQueues (/usr/app/node_modules/bluebird/js/release/async.js:17:14) at Immediate.args.(anonymous function) [as _onImmediate] (/usr/local/lib/node_modules/pm2/node_modules/event-loop-inspector/index.js:133:29) at 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' ]

最佳答案

阅读 Manish 的评论后,我决定重新尝试一下。

显然,实例方法的工作方式是在调用 .init 方法后初始化模型。

所以 -

let model = ModelClass.init(sequelize, Sequelize);
model = new model();

这将允许毫无问题地调用 model.modelMethod

关于javascript - 继承不存在的 ES6 模型方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52813078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com