question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sequelize with TypeScript - class fields undefined

See original GitHub issue

What are you doing?

I’am using Babel for code compilation + TypeScript for type-checking. Two scenarios:

  1. Using define, which is “really wouldn’t recommend this” (according types/test/define.ts comments)
interface User extends Model {
	id: number;
	login: string;
}

type UserModel = {
	new (): User;
} & typeof Model;

const User = sequelize.define(
	'User',
	{ login: DataTypes.STRING	},
	{ tableName: 'user' }
) as UserModel;
  1. Using new approach (based on code, found in the types/models/User.ts)
export class User extends Model {
	public id: number;
	public login: string;
}

User = User.init(
	{ login: DataTypes.STRING },
	{	sequelize, tableName: 'user' }
);

In DB (Postgres) I have one row with login ‘admin’.

For the test, I use this function.

async function test() {
	const user = (await User.findOne()) as User;
	console.log('login', user.login, user.get('login'));
}
test();

What do you expect to happen?

It is logical that in both scenarios I should get

login admin admin

What is actually happening?

However, if in “modern”, second approach I see

login undefined admin

And if I print console.log(user) I see

user1 User {
  dataValues:
   { id: 1,
     login: 'admin',
     createdAt: 2019-03-18T23:00:33.516Z,
     updatedAt: 2019-03-18T23:00:33.516Z },
  _previousDataValues:
   { id: 1,
     login: 'admin',
     createdAt: 2019-03-18T23:00:33.516Z,
     updatedAt: 2019-03-18T23:00:33.516Z },
  _changed: {},
  _modelOptions:
   { timestamps: true,
     validate: {},
     freezeTableName: false,
     underscored: false,
     paranoid: false,
     rejectOnEmpty: false,
     whereCollection: null,
     schema: null,
     schemaDelimiter: '',
     defaultScope: {},
     scopes: {},
     indexes: [],
     name: { plural: 'Users', singular: 'User' },
     omitNull: false,
     sequelize:
      Sequelize {
        options: [Object],
        config: [Object],
        dialect: [PostgresDialect],
        queryInterface: [QueryInterface],
        models: [Object],
        modelManager: [ModelManager],
        connectionManager: [ConnectionManager],
        importCache: {},
        test: [Object] },
     tableName: 'user',
     hooks: {} },
  _options:
   { isNewRecord: false,
     _schema: null,
     _schemaDelimiter: '',
     raw: true,
     attributes: [ 'id', 'login', 'createdAt', 'updatedAt' ] },
  isNewRecord: false,
  id: undefined,
  login: undefined }

Dialect: postgres “sequelize”: “^5.1.0”,

upd: Just tested with ts-node - everything works as it should. Here is my .babelrc

{
	"sourceMaps": "inline",
	"retainLines": true,

	"presets": [
		[
			"@babel/preset-env",
			{
				"targets": {
					"node": true
				}
			}
		],
		"@babel/preset-typescript"
	],
	"plugins": [
		"@babel/plugin-proposal-optional-chaining",
		"@babel/plugin-proposal-object-rest-spread",
		"@babel/proposal-class-properties"
	]
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:41 (6 by maintainers)

github_iconTop GitHub Comments

17reactions
pieczorxcommented, May 4, 2021

Please reopen

14reactions
MicroDroidcommented, Nov 30, 2020

Closed huh? Reopen this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring babel-typescript for Sequelize ORM causes ...
Columns should be retrieved through the Object.defineProperty() but when an existing property already exists it is skipped see source here. When ...
Read more >
TypeScript - Sequelize
As Sequelize heavily relies on runtime property assignments, TypeScript won't be very useful out of the box. A decent amount of manual type ......
Read more >
cannot read properties of undefined (reading 'findall') - You.com
when you use the sequelize model you need to use the defined model class name, which is "user" instead of "User" in line...
Read more >
sequelize-typescript - npm
A primary key ( id ) will be inherited from base class Model . This primary key is by default an INTEGER and...
Read more >
Understanding the exclamation mark in TypeScript
The non-null assertion operator tells the TypeScript compiler that a value typed as optional cannot be null or undefined .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found