sequelize-typescript: TypeError: Cannot read property 'createdAt' of undefined
Hello i´m getting this error “TypeError: Cannot read property ‘createdAt’ of undefined” do you have any idea what i can be?
Sequelize initialize
import {Sequelize} from 'sequelize-typescript';
const sequelize = new Sequelize(new Connection().Localhost());
/**
* Connections to dabase
*/
export class Connection {
private modelPaths = [__dirname + "/Models/Database"];
Development(){
return {
name: "db_a28301_escolar",
username: "a28301_escolar",
password: "N@escr14",
host:"mysql5013.smarterasp.net",
dialect:"mysql",
modelPaths: this.modelPaths
}
}
Production(){
return {
name: "db_production",
username: "user name",
password: "password",
host:"server/host",
dialect:"mysql",
modelPaths: this.modelPaths
}
}
Localhost(){
return {
name: "db_restapi",
username: "root",
password: "root",
host:"localhost",
dialect: "mysql",
modelPaths: this.modelPaths
}
}
}
Model class
import {Table, Column, Model, PrimaryKey,AutoIncrement, DataType} from 'sequelize-typescript';
/**
* reference docs: https://github.com/RobinBuschmann/sequelize-typescript
* Model that reflect on databse table.
*/
@Table({ tableName: 'place'})
export class Place extends Model<Place> {
@PrimaryKey
@AutoIncrement
@Column({ type: DataType.BIGINT})
id: number
@Column({ type: DataType.STRING(45)})
name: string
@Column({ type: DataType.STRING(45)})
description: string
}
ERROR LOG
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 53 (21 by maintainers)
@RobinBuschmann I know this is a closed Issue, but hopefully this might help someone else stumbling across this thread via Google (as I did).
I received this exact same error
TypeError: Cannot read property 'createdAt' of undefined
when I forgot to add a data model to the array passed to mysequelize.addModels()
call.This error is 100% me being careless, but the seemingly unrelated error message that @nullpixel mentioned did make it a bit difficult to track down!
Well is not that easy to explain , but would help me If you clone the project from https://github.com/TalissonJunior/Nodejs-WebApi. And try to run It.
So , i’m trying to get a list of “places” from my local database , acessing “Localhost:3000/api/places”, and to achieve that i have a controller.ts class that handles all the requests , then Business.ts class that handles the logic, and finally the repository.ts that will use the model to execute a query.
The error occurs when my Business class tries to use the repository passing my Model to get the places from my database.
I know that i’m not explaining It well , but i would really appreciate some help to figure out this error.
I left a dump file If my database inside database_scripts folder in case you clone the repository.
To test the project you just need to run “npm install” and “gulp serve”.
@doovers As stated before you need to set target to
es6
when using es6 classes. Furthermore you need to tell the typescript compiler how to resolve the modules. So the following configuration works for me:I’ve noticed several issues: You’re creating an instance of
Model
in your BaseRepository. If you want to create instances of your models, you rather should callnew
on your specific models likePlace
(=>new Place(...)
) than onModel
(new Model()
) -Model
is an abstractclass
.Additionally this is not what you meant to do here. You want to use
find
,findAll
,create
and so on, which are static functions of the model class, not of the instances. So thatBesides that, you are never passing the
Place
model class inPlaceRepository
to theBaseRepository
. I assume you want something like this:@adamtLICOR: yeah, the error is terrible. makes these bugs really hard to track down!
@RobinBuschmann Awesome! Thanks for figuring that out. My problem was I was missing the module & module resolution params in the tsconfig! Do you have any insight on the other problem of having the phone model as a child of customer twice? As a fax and phone property?