Issue: Unable to call method "call" as the model "Model" has not been initialized within a Sequelize instance. Kindly ensure that "Model" is added to a Sequelize instance before attempting to use the "call" method

Author.ts

import {Table, Model, Column, DataType} from 'sequelize-typescript'

@Table
export class Author extends Model<Author> {

constructor(){
    super();
}

@Column(DataType.STRING)
fname: string

@Column(DataType.STRING)
lname: string

}

App.ts

import {Sequelize} from 'sequelize-typescript';
import { customer } from './model/customer';
import { Author } from './model/Author';

export  class SQLRepo {

     repo:Sequelize = null;

     constructor(){
         this.connectDb();
     }

  connectDb(): any {

    this.repo =  new Sequelize({
        database: 'postgres',
        dialect: 'postgres',
        host: 'localhost',
        username: 'postgres',
        password: 'xxxxx',
        storage: 
               'D:/Sequalize/assosiation/database.sqlite',
        modelPaths: [__dirname + '/models'],
        define: {
            underscored: false,
            freezeTableName: false,
            timestamps: false
        }
     });

     this.repo.addModels([Author])

     this.repo.
     authenticate().
     then(function(){
         console.log("database connected ...")

         new Author({fname:'karim', lname:'mirazul'}).save();

     }).
     catch(function(error){
         console.log("Database catch block : "+ error)
     })
   }
}
new SQLRepo();

Create a SQLRepo call object to successfully establish a connection with the database, however an error occurs after DB connection is established when trying to insert data into the Author table.

The following output is displayed:

Executing (default): SELECT 1+1 AS result database connected ... Database catch block :

Error: Model not initialized: "Model" needs to be added to a Sequelize instance before "call" can be executed.

Answer №1

Eliminate constructor invocation from the model definition

import {Table, Model, Column, DataType} from 'sequelize-typescript'

@Table
export class Author extends Model<Author> {

  // constructor(){
  //     super();
  // }

  @Column(DataType.STRING)
  fname: string

  @Column(DataType.STRING)
  lname: string

}

Avoid using the constructor method as it is reserved for internal operations by sequelize

source

Answer №2

By making the switch from target: "es5" to target: "es6", I was able to successfully resolve the problem at hand. This specific solution is not officially documented, but it can be found in the provided example.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible for me to declare an Array once and then declare it again at different points in the code?

Is it possible to re-declare an Array that has already been declared? I'm attempting to iterate through a LinkedList and identify every index containing "null" as an Element, then add those indexes to an array of integers. The issue I'm facing ...

Listening for a long press in Angular 2: A step-by-step guide

Check out this link for an AngularJS example. Curious how this functionality translates to Angular 2? ...

Passing a boolean as the value for a Material UI MenuItem in a React TypeScript application

I am encountering an issue as a beginner in react with passing a simple boolean value as a prop for the MenuItem component in the material UI library. I believe the solution is not too complex. Can someone provide guidance on how to fix this error? The sp ...

`In NodeJS, facing a challenge with implementing a many-to-many relationship using Sequelize's

I'm in the process of setting up many-to-many relationships between roles and accesses. The `roles` table will contain a list of roles (admin, developer, etc...) and the `accesses` table will have a list of permissions (Create Project, Create Site, De ...

What sets apart the Partial and Optional operators in Typescript?

interface I1 { x: number; y: string; } interface I2 { x?: number; y?: string; } const tmp1: Partial<I1> = {}, tmp2: I2 = {}; Can you spot a clear distinction between these two entities, as demonstrated in the above code snippet? ...

Encountering Typescript errors while compiling an Angular module with AOT enabled

I am currently in the process of manually constructing an Angular module with Webpack, opting not to use the CLI. While a normal build is functioning without any issues, encountering errors during an AOT build! Here's how my tsconfig.aot.json file ...

No pathways can be established within Core UI Angular

I've been attempting to use the router link attribute to redirect to a new page, but instead of landing on the expected page, I keep getting redirected to the dashboard. Below is an overview of how my project's structure looks: [![enter image de ...

Arranging elements within an outer array by the contents of their inner arrays

I need help organizing an array based on the alphabetical order of a specific value within the inner arrays. For example: I want to sort this array by the prefix "old," so old A, old B, etc. const array = [ { personName: "Vans", personTags: ["young", " ...

Angular 2 - Ensuring service executes only when boolean condition is met

I am currently dealing with a navigation menu that utilizes the ng2-page-scroll module. When scrolling through the page using hashtag links, I encountered an issue. If I navigate across routes, there is a delay in loading the data. As a result, the servic ...

The functionality of TypeScript's instanceof operator may fail when the method argument is not a simple object

There seems to be an issue with a method that is being called from two different places but returns false for both argument types. Despite checking for the correct types, the problem persists and I am unsure why. Although I have read a similar question on ...

There was a parsing error due to encountering an unexpected reserved word 'interface' in the code, as flagged

I'm encountering an issue with my code when trying to utilize Props. The error message I'm receiving is "Parsing error: Unexpected reserved word 'interface'. (3:0)eslint". This project is being developed using next with TypeScript. Er ...

We could not locate the export in Typescript, and the JSX element type does not show any construct or call signatures

Looking to utilize my Typescript React Component Library as a Package. The structure of my files is as follows: MyComponent.tsx: import React, { FunctionComponent } from 'react'; import styled from 'styled-components'; export interf ...

Dispatching an asynchronous function error in React with TypeScript and Redux - the parameter type is not assignable to AnyAction

Currently, I am in the process of developing a web application that utilizes Firebase as its database, along with Redux and TypeScript for state management. Within my code, I have a dispatch function nested inside a callback function like so: export const ...

What is the best way to generate a JSON object with Angular and showcase its content through HTML?

Currently, I am dealing with a JSON object that is completely unfamiliar to me. Without knowing the keys or values of this object, I was able to successfully manipulate it and extract the necessary information. Ultimately, I have generated an array (whic ...

Ensuring that the app closes completely before launching a new instance with webpack hot module reload

My nest.js application is utilizing webpack hot module reload (hmr), but I am encountering an issue where the reload does not allow the old instance to fully close (including the database connection and telegram bot) before launching a new instance. This c ...

Issue with destructuring in function parameter in TSLint code analysis

I'm trying to resolve the tslint error that occurs in the object destructuring parameter of this code snippet: export function renameProperty( oldProp: string, newProp: string, {[oldProp]: old, ...others} ): any { return { [ne ...

How do I add a new item to an object using Ionic 2?

example item: this.advData = { 'title': this.addAdvS2.value.title , 'breadcrumb': this.suggestData.breadcrumb, 'price': this.addAdvS2.value.price ...

What is the best way to limit the type of the second argument based on the type of the

Within the tutorial Exploring How to Extract Parameter Types from String Literal Types Using TypeScript, a fascinating problem is presented without a solution. function calculate(operation, data) { if (operation === 'add') { return da ...

Unexpected token { in Fuse-Box when using Typescript

Here's the beginning of my fuse.ts file import { CSSPluginOptions } from 'fuse-box/plugins/stylesheet/CSSplugin'; import { argv } from 'yargs'; import * as path from 'path'; import { CSSPlugin, CSSResourcePlugin, Env ...

Handling a change event for a mat-datepicker in Angular 7 can be tricky, especially when its value is tied to an optional input parameter. Let's dive into how to

I've recently started working with angular development and encountered a challenge with a mat-datepicker. The value of the datepicker is connected to filterDate using [(ngModel)] as an @Input() parameter. I have implemented a handleChange event that e ...