Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
How to pass variable into init in a JavaScript program

I have a model named user.js that uses Sequelize:

const { Sequelize, Model, STRING,} = require('sequelize');

var sequelizeCredentials = null;

class User extends Model{
    constructor(sequelizeCredentials){
        this.sequelizeCredentials = sequelizeCredentials;
    }
};
console.log(sequelizeCredentials);
User.init(
    {
        id: {type:Sequelize.UUID, defaultValue: Sequelize.UUIDV4, primaryKey:true},
        username: {type:STRING, allowNull:false},
        password: {type:STRING, allowNull:false},
        name: {type:STRING, allowNull:false},
    },{sequelize: sequelizeCredentials, modelName:'user'}
);
module.exports = { User };

I am trying to pass my sequelize credentials from the app.js like this:

const express = require('express');
const { Sequelize } = require('sequelize');
const mysql = require('mysql2/promise');
const user = require('./modals/user');
const app = express();

app.use(express.json());

initialize();
async function initialize() {
   const sequelize = new Sequelize(
        'express_project',
        'root',
        '',
        {
            dialect: 'mysql'
        }
    );

    localUser = new user.User(sequelize);

    await sequelize.sync();
}

However, I keep getting an error saying "No Sequelize instance passed".

I am very new to JS/TS.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

try this

const { Sequelize, Model, STRING,} = require('sequelize');

class User extends Model{
  constructor(){
    super();
  }
  static init(sequelize) {
   return super.init({
     id: {type:Sequelize.UUID, defaultValue: Sequelize.UUIDV4, primaryKey:true},
     username: {type:STRING, allowNull:false},
     password: {type:STRING, allowNull:false},
     name: {type:STRING, allowNull:false},
    },{sequelize, modelName:'user'});
   }
}
module.exports = { User };

in app.js

async function initialize() {
  const sequelize = new Sequelize(
    'express_project',
    'root',
    '',
    {
        dialect: 'mysql'
    }
);

User.init(sequelize);

await sequelize.sync();

}

more od es6 classes with sequlize check this out https://codewithhugo.com/using-es6-classes-for-sequelize-4-models/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!