Sequelize-typescript: Sequelize-typescript cannot use include in query

Created on 19 Mar 2019  路  12Comments  路  Source: RobinBuschmann/sequelize-typescript

Hi,

Every time I try to get a simple query, I get the next error

TypeError: Cannot read property 'include' of undefined
at Model.get (/admin/node_modules/sequelize/lib/model.js:3248:66)
at Model.toJSON (/admin/node_modules/sequelize/lib/model.js:4101:12)
at JSON.stringify ()
at stringify (/admin/node_modules/fast-safe-stringify/index.js:11:18)
at Format.module.exports.format [as transform] (/admin/node_modules/logform/json.js:24:19)
at Format.info [as transform] (/admin/node_modules/logform/combine.js:20:24)
at DerivedLogger._transform (/admin/node_modules/winston/lib/winston/logger.js:305:29)
at DerivedLogger.Transform._read (/admin/node_modules/winston/node_modules/readable-stream/lib/_stream_transform.js:177:10)
at DerivedLogger.Transform._write (/admin/node_modules/winston/node_modules/readable-stream/lib/_stream_transform.js:164:83)
at doWrite (/admin/node_modules/winston/node_modules/readable-stream/lib/_stream_writable.js:405:139)
at writeOrBuffer (/admin/node_modules/winston/node_modules/readable-stream/lib/_stream_writable.js:394:5)
at DerivedLogger.Writable.write (/admin/node_modules/winston/node_modules/readable-stream/lib/_stream_writable.js:303:11)
at DerivedLogger.log (/admin/node_modules/winston/lib/winston/logger.js:244:14)
at DerivedLogger.(anonymous function) (/admin/node_modules/winston/lib/winston/create-logger.js:95:19)
at Sequelize.log (/admin/node_modules/sequelize/lib/sequelize.js:1091:23)
at Query.run (/admin/node_modules/sequelize/lib/dialects/mysql/query.js:52:22)
at Promise.try.then.connection (/admin/node_modules/sequelize/lib/sequelize.js:558:20)
at tryCatcher (/admin/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/admin/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/admin/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/admin/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/admin/node_modules/bluebird/js/release/promise.js:694:18)

these are the models

import { Model, PrimaryKey, AllowNull, Column, DataType, CreatedAt, UpdatedAt, Table, HasOne } from "sequelize-typescript";
import AdminUser from "../models/AdminUser";

@Table({ tableName: 'establishment', paranoid: false, timestamps: true })
export default class IEstablishment extends Model<IEstablishment> {
  @PrimaryKey
  @AllowNull(false)
  @Column(DataType.INTEGER)
  id : number;

  @CreatedAt
  @AllowNull(false)
  @Column(DataType.DATE)
  created_at: Date; 

  @AllowNull(false)
  @Column(DataType.INTEGER)
  status: number;

  @UpdatedAt
  @AllowNull(false)
  @Column(DataType.DATE)
  updated_at: Date;

  @AllowNull(false)
  @Column(DataType.STRING(100))
  name: string;

  @HasOne(() => AdminUser)
  adminUSer: AdminUser
}
import { Model, Table, PrimaryKey, AutoIncrement, Unique, AllowNull, Column, DataType, CreatedAt, DeletedAt, UpdatedAt, ForeignKey, BelongsTo } from "sequelize-typescript";
import Establishment from "../models/Establishment";

@Table({ paranoid: true, timestamps: true, tableName: 'admin_user' })
export default class IAdminUser extends Model<IAdminUser> {
  @PrimaryKey
  @AutoIncrement
  @Unique
  @AllowNull(false)
  @Column(DataType.INTEGER)
  id: number;

  @Unique
  @AllowNull(false)
  @Column(DataType.STRING(200))
  email: string;

  @AllowNull(false)
  @Column(DataType.STRING(150))
  password: string;

  @CreatedAt
  @Column(DataType.DATE)
  created_at: Date;

  @DeletedAt
  @Column(DataType.DATE)
  deleted_at: Date;

  @UpdatedAt
  @Column(DataType.DATE)
  updated_at: Date

  @ForeignKey(() => Establishment)
  @Column(DataType.INTEGER)
  establishment_id: number;

  @BelongsTo(() => Establishment)
  establishment: Establishment;
}
import IAdminUser from "../db_interfaces/IAdminUser";
import Establishment from "./Establishment";

export default class AdminUser extends IAdminUser {
  static async findUserById(id: number) {
    return await this.findByPrimary(id, {
      include: [Establishment]
    });
  };
}

```ts
import IEstablishment from "../db_interfaces/IEstablishment";

export default class Establishment extends IEstablishment {

}

and this is the query

  ```ts
try {
    let user = await AdminUser.findUserById(1);
    return res.send(user);
  } 
  catch(e) {
    logger.error(e);
    return cb(e);
  }
});

Edit: Also when i run the query without the include key it works normally

Thank you in advance

All 12 comments

@fccruz Can you provide an example repo, which reproduces the issue? I couldn't.

@fccruz try return res.send(user.toJSON());

Did you manage to solve the problem? I have the same issue.

@morrigan Can you provide an example repo which reproduces the issue?

@morrigan did you try return res.send(user.toJSON()); ?
I had exactly the same problem and managed to avoid it simply by running toJSON() on the instance first.

happens to me on the latest version after doing toJSON or it equivalent get({plain: true})

@morrigan @OrenSchwartz I've released a new beta version (npm i [email protected]) which should fix at least the issue morrigan had. Can you confirm?

@RobinBuschmann I was having the same issue, testing the beta now. I'll let you know the results in the next 24 hours :)

@RobinBuschmann Fix seems to be good, no exceptions in the past 29 hours of using it :) , thanks.

fix seems to be good.

I'm closing this, since the fix was released with [email protected]

Was this page helpful?
0 / 5 - 0 ratings