'What am I doing wrong with Sequelize?
I'm trying to use Sequelize for my simple Typescript application. I simply want to connect to Postgres db via Sequelize (I can) and create a simple table Book
(I can't)
I've the file interface.ts
within which I've the interface book
:
export interface book{
title: string;
author: string;
price: number;
id: number;
}
Then I've the database.ts
file:
import { Dialect, Sequelize } from 'sequelize'
import {book, purchases, user} from "../interface"
import { DataTypes, Model, Optional } from 'sequelize'
const dbName = process.env.DB_NAME as string
const dbUser = process.env.DB_USER as string
const dbHost = process.env.DB_HOST
const dbDriver = process.env.DB_DRIVER as Dialect
const dbPassword = process.env.DB_PASSWORD
export const sequelizeConnection = new Sequelize(dbName, dbUser, dbPassword, {
host: dbHost,
dialect: "postgres"
});
class Book extends Model<book> {
id!: number;
author!: string;
price!: number;
title!: string;
}
export function dbInit (){
Book.sync({force: true});
}
In the main file, index.ts
:
async function Start() {
try {
await sequelizeConnection.sync();
dbInit();
app.listen(configuration.server_port, (): void => {
console.log("Server listening on port " + configuration.server_port);
});
} catch (error) {
console.log("Error occured: " + error);
}
}
Start();
The error I obtain is:
Executing (default): SELECT 1+1 AS result
Server listening on port 3000
(node:577) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'hooks' of undefined
at getHooks (/mnt/c/Users/raffa/Desktop/esercitazione_ts/node_modules/sequelize/src/hooks.js:70:26)
at Function.runHooks (/mnt/c/Users/raffa/Desktop/esercitazione_ts/node_modules/sequelize/src/hooks.js:98:15)
at Function.sync (/mnt/c/Users/raffa/Desktop/esercitazione_ts/node_modules/sequelize/src/model.js:1339:18)
at dbInit (/mnt/c/Users/raffa/Desktop/esercitazione_ts/src/config/dabatase.ts:25:8)
at /mnt/c/Users/raffa/Desktop/esercitazione_ts/src/index.ts:18:11
at step (/mnt/c/Users/raffa/Desktop/esercitazione_ts/src/index.ts:56:23)
at Object.next (/mnt/c/Users/raffa/Desktop/esercitazione_ts/src/index.ts:37:53) at fulfilled (/mnt/c/Users/raffa/Desktop/esercitazione_ts/src/index.ts:28:58)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:577) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,
or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:577) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
What am I doing wrong?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|