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

Struggling to Enforce Restricted Imports in TypeScript Project Even After Setting baseUrl and resolve Configuration

I am facing challenges enforcing restricted imports in my TypeScript project using ESLint. The configuration seems to be causing issues for me. I have configured the baseUrl in my tsconfig.json file as "src" and attempted to use modules in my ESLint setup ...

What is the process for inputting a predefined function into an interface?

In my project, I have a Locale interface that defines the properties of a locale for my component: interface Locale { src: string; alt: string; language: string; i18nFormat: string; } During debugging, I am using the built-in .toSource() function ...

Is it possible to utilize useEffect for verifying the existence of the user token within the localStorage?

I am in the process of developing a web application that requires authentication. I am wondering if it is effective to create a private route by adding a condition in the useEffect hook of one of my pages. The idea is to check if a token is present before ...

How to Establish an Angular Global Variable

What is the process for establishing a global variable in Angular? I have established a variable within a service, entered a value in the login component, and attempted to access this variable from another component. However, I noticed that the value res ...

The element is inferred to have an 'any' type due to the inability to use a 'string' type expression to index the 'Palette' type

Encountering an issue: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Palette'. No index signature with a parameter of type 'string' was found on type &ap ...

Is there a way to make the vss sdk treeview collapse automatically?

Is there a way to have the nodes of a combo control of type TreeView.ComboTreeBehaviorName collapsed by default? I've searched through the documentation at this link and this link, but couldn't find a solution. I also examined the types in vss. ...

Error: Cannot access Angular 5 Title service at this time

When attempting to change the page title using BrowserModule, I encountered an issue. I added the BrowserModule and Title in the application module as shown here: https://angular.io/guide/set-document-title However, in a child module where I tried to add ...

Remove all input fields within an HTML file using a TypeScript method implemented in an Angular 2 component

Within my Angular project, there are several input elements in the HTML file that are not enclosed within a form tag. I am looking to create a function in the TypeScript file that will clear all of these inputs. I attempted to utilize ViewChild, but it a ...

The optimal time to register for events within the Vue lifecycle

Currently, I am developing a Vue2 component using vue-component that includes a subcomponent. Here is an example: <note :bus="bus" :itemId="selectedId"></note> The subcomponent contains the following code snippet: <textarea v-model="text" ...

How to Animate the Deletion of an Angular Component in Motion?

This stackblitz demonstration showcases an animation where clicking on Create success causes the components view to smoothly transition from opacity 0 to opacity 1 over a duration of 5 seconds. If we clear the container using this.container.clear(), the r ...

Troubleshooting Missing Exports from Local Modules in React Vite (Transitioning from Create React App)

I have encountered an issue while trying to import exported members from local nodejs packages in my project. Everything was working fine with the standard CRA webpack setup, but after switching to Vite, I started getting the following error: Unca ...

Facing a problem with querying interfaces and types in TypeScript

My goal is to pass a variable to an RTK Query API service that uses a typescript interface: const device_id: unique symbol = Symbol(props.id); const { data: device, isFetching, isLoading, } = useGetAssetsByIdQuery(device_id, { pollingInterv ...

Upon deployment, Angular encounters issues with routing to lazy loaded modules

I recently completed development on a new Angular application that utilizes lazy loading for improved performance. During local testing using ng serve (or ng serve --prod to mimic production mode), the app compiled without errors and functioned properly. ...

Strategies for avoiding unused style tags in React components

Expanding beyond React, I'm unsure if React itself is the culprit of this issue. In a React environment with TypeScript, I utilize CSS imports in component files to have specific stylesheets for each component. I assumed these styles would only be ad ...

Utilize various interfaces for a single object

I'm working on a Typescript project where I need to pass the same object between multiple functions with different interfaces. These are the interfaces: export interface TestModel { fileName:string, year:number, country:string } export interfac ...

The term 'components' has not been defined (no-undef)

Recently integrated Vue into an existing project and encountered a peculiar linting error: error: 'components' is not defined (no-undef) at src/App.vue:13:3: 11 | 12 | @Component({ > 13 | components: { HelloWorld }, | ^ 14 | }) ...

typescript - specifying the default value for a new class instance

Is there a way to set default values for properties in TypeScript? For example, let's say we have the following class: class Person { name: string age: number constructor(name, age){ this.name = name this.age = age } } We want to ens ...

Error: The function list.forEach does not exist within service.buildList [as project]

Today, I've been grappling with a challenging issue. As someone new to Typescript and Angular, I'm attempting to make a call to my backend API. However, when trying to populate an array for display, I keep encountering an error that says rawRegis ...

forEach was unable to determine the appropriate data types

define function a(param: number): number; define function b(param: string): string; define function c(param: boolean): boolean; type GeneralHooks<H extends (...args: any[]) => any> = [H, Parameters<H>] const obj = { a: [a, [1]] as Gene ...

Exploring Angular 4.3's HTTP Interceptor Retry功能

As I delve into my first attempt at coding, I find myself faced with the challenge of capturing 401 errors using HttpInterceptor. My goal is to generate a new auth token based on a certain condition and then retry the process with that token in place. Howe ...