- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用命令“yarn tsc”转译代码后,我尝试执行我的代码并得到下图中的错误。当我去检查错误原因时,我看到我的 Sequelize 模型类没有被堆叠,它们是空的。我无法在任何地方找到有关此原因的解释,也无法在 Sequelize documentation 中找到解释。有人可以帮忙吗?
我的依赖
"dependencies": {
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@types/bcrypt": "^3.0.0",
"@types/cors": "^2.8.6",
"@types/jsonwebtoken": "^8.5.0",
"@types/moment": "^2.13.0",
"@types/nodemailer": "^6.4.0",
"@types/ws": "^7.2.4",
"axios": "^0.19.2",
"bcrypt": "^4.0.1",
"cep-promise": "^3.0.9",
"class-transformer": "^0.2.3",
"class-validator": "^0.12.2",
"cors": "^2.8.5",
"discord.js": "^12.2.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"jwt-then": "^1.0.1",
"melif": "^1.0.6",
"moment": "^2.25.3",
"mysql2": "^2.1.0",
"nodemailer": "^6.4.6",
"reflect-metadata": "^0.1.13",
"sequelize": "^5.21.5",
"sequelize-cli": "^5.5.1",
"sequelize-cli-typescript": "^3.2.0-c",
"ts-node": "^8.10.1",
"typescript": "^3.9.2"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.6",
"@babel/plugin-transform-runtime": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@babel/preset-typescript": "^7.9.0",
"@babel/register": "^7.9.0",
"@types/bluebird": "^3.5.30",
"@types/express": "^4.17.6",
"@types/node": "^14.0.1",
"@types/sequelize": "^4.28.9",
"@types/validator": "^12.0.1",
"@typescript-eslint/eslint-plugin": "^2.33.0",
"@typescript-eslint/parser": "^2.33.0",
"eslint": "^7.0.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"nodemon": "^2.0.4",
"sucrase": "^3.15.0",
"typescript": "^3.9.2"
}
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
//"esModuleInterop": true,
"resolveJsonModule": true,
"experimentalDecorators": true,
"outDir": "./dist",
"target": "es6",
//"noEmit": true,
//"allowSyntheticDefaultImports": true,
"types": [
"node"
]
},
"include": [
"src"
],
"exclude": [
"node_modules"
],
"compileOnSave": false
}
import { Sequelize, Model, DataTypes, HasManyGetAssociationsMixin, HasManyAddAssociationMixin, HasManyHasAssociationMixin, Association, HasManyCountAssociationsMixin, HasManyCreateAssociationMixin, BelongsToManyGetAssociationsMixin, BelongsToManyAddAssociationMixin, BelongsToManyHasAssociationMixin, BelongsToManyCountAssociationsMixin, BelongsToManyCreateAssociationMixin, HasManyRemoveAssociationMixin } from 'sequelize'
import { Route } from './Route'
import { Company } from './Company'
import { Account } from './Account'
import { DoneRoute } from './DoneRoute'
import { DoneLesson } from './DoneLesson'
import { Payment } from './Payment'
export interface UserI {
id?: number | null;
name: string;
email: string;
password: string;
introduced: boolean;
paid_access_expiration: Date;
}
export class User extends Model implements UserI {
public id?: number | null;
public name!: string;
public email!: string;
public password!: string;
public introduced!: boolean;
public paid_access_expiration!: Date;
public getRoutes!: BelongsToManyGetAssociationsMixin<Route>; // Note the null assertions!
public addRoute!: BelongsToManyAddAssociationMixin<Route, number>;
public hasRoute!: BelongsToManyHasAssociationMixin<Route, number>;
public removeRoute!: HasManyRemoveAssociationMixin<Route, number>;
public countRoutes!: BelongsToManyCountAssociationsMixin;
public createRoute!: BelongsToManyCreateAssociationMixin<Route>;
public getCompanies!: BelongsToManyGetAssociationsMixin<Company>; // Note the null assertions!
public addCompany!: BelongsToManyAddAssociationMixin<Company, number>;
public hasCompany!: BelongsToManyHasAssociationMixin<Company, number>;
public removeCompany!: HasManyRemoveAssociationMixin<Company, number>;
public countCompanies!: BelongsToManyCountAssociationsMixin;
public createCompany!: BelongsToManyCreateAssociationMixin<Company>;
public getDoneRoutes!: HasManyGetAssociationsMixin<DoneRoute>; // Note the null assertions!
public addDoneRoute!: HasManyAddAssociationMixin<DoneRoute, number>;
public hasDoneRoute!: HasManyHasAssociationMixin<DoneRoute, number>;
public removeDoneRoute!: HasManyRemoveAssociationMixin<DoneRoute, number>;
public countDoneRoutes!: HasManyCountAssociationsMixin;
public createDoneRoute!: HasManyCreateAssociationMixin<DoneRoute>;
public getDoneLessons!: HasManyGetAssociationsMixin<DoneLesson>; // Note the null assertions!
public addDoneLesson!: HasManyAddAssociationMixin<DoneLesson, number>;
public hasDoneLesson!: HasManyHasAssociationMixin<DoneLesson, number>;
public removeDoneLesson!: HasManyRemoveAssociationMixin<DoneLesson, number>;
public countDoneLessons!: HasManyCountAssociationsMixin;
public createDoneLesson!: HasManyCreateAssociationMixin<DoneLesson>;
public getAccounts!: HasManyGetAssociationsMixin<Account>; // Note the null assertions!
public addAccount!: HasManyAddAssociationMixin<Account, number>;
public hasAccount!: HasManyHasAssociationMixin<Account, number>;
public removeAccount!: HasManyRemoveAssociationMixin<Account, number>;
public countAccounts!: HasManyCountAssociationsMixin;
public createAccount!: HasManyCreateAssociationMixin<Account>;
public getPayments!: HasManyGetAssociationsMixin<Payment>; // Note the null assertions!
public addPayment!: HasManyAddAssociationMixin<Payment, number>;
public hasPayment!: HasManyHasAssociationMixin<Payment, number>;
public removePayment!: HasManyRemoveAssociationMixin<Payment, number>;
public countPayments!: HasManyCountAssociationsMixin;
public createPayment!: HasManyCreateAssociationMixin<Payment>;
public readonly routes?: Route[];
public readonly companies?: Company[];
public readonly done_routes?: DoneRoute[];
public readonly done_lessons?: DoneLesson[];
public readonly accounts?: Account[];
public readonly payments?: Payment[];
// timestamps!
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
public static associations: {
routes: Association<User, Route>;
companies: Association<User, Company>;
done_routes: Association<User, DoneRoute>;
done_lessons: Association<User, DoneLesson>;
accounts: Association<User, Account>;
payments: Association<User, Payment>;
};
}
export function init (sequelize: Sequelize): void {
User.init(
{
id: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
email: {
type: DataTypes.STRING,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false
},
introduced: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: true
},
paid_access_expiration: {
type: DataTypes.DATE,
allowNull: false
}
// Colocar Relações
},
{
tableName: 'users',
sequelize: sequelize // this bit is important
}
)
}
export function associate (sequelize: Sequelize): void {
User.belongsToMany(sequelize.models.Route, { foreignKey: 'user_id', through: 'user_routes', as: 'routes' })
User.belongsToMany(sequelize.models.Company, { foreignKey: 'user_id', through: 'user_companies', as: 'companies' })
User.hasMany(sequelize.models.DoneLesson, { foreignKey: 'user_id', as: 'done_lessons' })
User.hasMany(sequelize.models.DoneRoute, { foreignKey: 'user_id', as: 'done_routes' })
User.hasMany(sequelize.models.Account, { foreignKey: 'user_id', as: 'accounts' })
User.hasMany(sequelize.models.Payment, { foreignKey: 'user_id', as: 'payments' })
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.associate = exports.init = exports.User = void 0;
const sequelize_1 = require("sequelize");
class User extends sequelize_1.Model {
}
exports.User = User;
function init(sequelize) {
User.init({
id: {
type: sequelize_1.DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true
},
name: {
type: sequelize_1.DataTypes.STRING,
allowNull: false
},
email: {
type: sequelize_1.DataTypes.STRING,
allowNull: false
},
password: {
type: sequelize_1.DataTypes.STRING,
allowNull: false
},
introduced: {
type: sequelize_1.DataTypes.BOOLEAN,
defaultValue: false,
allowNull: true
},
paid_access_expiration: {
type: sequelize_1.DataTypes.DATE,
allowNull: false
}
// Colocar Relações
}, {
tableName: 'users',
sequelize: sequelize // this bit is important
});
}
exports.init = init;
function associate(sequelize) {
User.belongsToMany(sequelize.models.Route, { foreignKey: 'user_id', through: 'user_routes', as: 'routes' });
User.belongsToMany(sequelize.models.Company, { foreignKey: 'user_id', through: 'user_companies', as: 'companies' });
User.hasMany(sequelize.models.DoneLesson, { foreignKey: 'user_id', as: 'done_lessons' });
User.hasMany(sequelize.models.DoneRoute, { foreignKey: 'user_id', as: 'done_routes' });
User.hasMany(sequelize.models.Account, { foreignKey: 'user_id', as: 'accounts' });
User.hasMany(sequelize.models.Payment, { foreignKey: 'user_id', as: 'payments' });
}
exports.associate = associate;
最佳答案
很久以前我认为我遇到了这个问题 - 但尽管如此 - 发布我(模糊地)记得的解决方案可能仍然有帮助。
我的模型位于“models”文件夹中 - 因此我从编译该文件夹中排除了 @babel/plugin-proposal-class-properties
babel 插件。
我不记得为什么设置了 loose: true
选项。尝试使用/不使用它,看看它是否会改变行为。
记录了一个错误 - https://github.com/sequelize/sequelize/issues/10579 - 据称已关闭(截至 2021 年 1 月)-但似乎并没有真正解决。
{
"env": {
"development": {
"presets": [
[
"next/babel"
]
],
"plugins": [
[
"@babel/plugin-proposal-class-properties",
{
"exclude": "models",
"loose": true
}
]
]
},
"production": {
"presets": [
"next/babel"
],
"plugins": [
[
"@babel/plugin-proposal-class-properties",
{
"exclude": "models",
"loose": true
}
]
]
},
...
}
关于node.js - 如何让 Typescript 转译我的 Sequelize 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61960885/
我已经写了并且 npm 发布了这个:https://github.com/justin-calleja/pkg-dependents 现在我正在用 Typescript 编写这个包:https://g
我有一个函数,我想在 TypeScript 中模拟它以进行测试。在我的测试中,我只关心 json和 status .但是,当使用 Jest 的 jest.spyOn 时我的模拟函数的类型设置为返回 h
我正在使用一个库 (Axios),它的包中包含 Typescript 声明。 我想声明一个将 AxiosResponse(在库的 .d.ts 文件中声明)作为参数的函数。我有以下内容: functio
我是 Typescript 的新手。我想使用 将一个 Typescript 文件加载到另一个 Typescript 文件中标签。 我做了一些事情,但它不起作用!请帮助我。 first.ts: imp
为什么我会收到下面屏幕截图中显示的错误? Atom 说我的 tsconfig.json“项目文件包含无效选项”用于 allowJs、buildOnSave 和 compileOnSave。 但是应该允
所以我正在创建一个 TypeScript 库,我可以轻松地将所有生成的 JS 文件编译成一个文件。有没有办法将所有 .ts 和 .d.ts 编译成一个 .ts 文件? 除了支持 JS 的版本(较少的智
Microsoft Research 提供了一种名为Safer TypeScript 的新 TypeScript 编译器变体: http://research.microsoft.com/en-us/
我需要这个来在单个文件中分发 TypeScript 中的库。有没有办法将多个 typescript 文件合并到(一个js文件+一个 typescript 定义)文件中? 最佳答案 要创建一个库,您可以
用例:我想知道一个函数在 typescript 中执行需要多少时间。我想为此目的使用装饰器。我希望装饰器应该返回时间以便(我可以进一步使用它),而不仅仅是打印它。 例如: export functio
我想检查一个类型是否可以为 null,以及它是否具有值的条件类型。 我尝试实现 type IsNullable = T extends null ? true : false; 但是好像不行 type
我的问题是基于这个 question and answer 假设我们有下一个代码: const myFn = (p: { a: (n: number) => T, b: (o: T) => v
我知道双重否定前缀,我知道 TypeScript 的单后缀(非空断言)。 但是这个双后缀感叹号是什么? /.*验证码为(\d{6}).*/.exec(email.body!!)!![1] 取自here
我正在使用以下文件结构在 Webstorm 中开发一个项目 | src | ... | many files | types | SomeInterface |
在 TypeScript 类中,可以为属性声明类型,例如: class className { property: string; }; 如何在对象字面量中声明属性的类型? 我试过下面的代码,但它
我正在寻找一种在不丢失推断类型信息的情况下将 TypeScript 中的文字值限制为特定类型的好方法。 让我们考虑一个类型Named,它保证有一个名字。 type Named = { name:
在 TypeScript 中,我想创建一个联合类型来表示属于一个或多个不同类型的值,类似于 oneOf在 OpenAPI或 JSON Schema .根据a previous answer on a
type Func = (foo:string) => void // function expression const myFunctionExpression:Func = function(f
假设我有一个联合类型,我正在使用类似 reducer 的 API 调用模式,看起来像这样: type Action = { request: { action: "create
我在 typescript 中有以下去抖功能: export function debounce( callback: (...args: any[]) => void, wait: numb
在 Vue3 的 defineComponent 函数中,第一个泛型参数是 Props,所以我在这里使用 Typescript 接口(interface)提供我的 props 类型。喜欢: expor
我是一名优秀的程序员,十分优秀!