Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
Exported sequelize instance is undefined in Model imports

Good evening.

I have an express server. The main script of the server has the following export:

export const sequelize = new Sequelize(
  'postgres', config.db_user, config.db_password, {
    host: 'localhost',
    port: config.db_port,
    dialect: 'postgres'
  })

And I'm trying to create a file structure that allows for each model to be define in a different file. Right now I have a Sequelize model defined like this:

import { Test } from "@api-models";
import { DataTypes } from "sequelize";
import { sequelize } from "../main";

Test.init({
    id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false
    },
    joinedDate: {
        type: DataTypes.DATE
    }
}, {sequelize: sequelize})

export default Test

But this doesn't work, as the sequelize import in the model file returns undefined, and the initialization fails with the following error message:

Error: No Sequelize instance passed

I've been investigating for a while, but most answers seem to rely on node or JS exporting syntax to specify the order of execution, rather than ES6 imports/exports. What would be the proper approach to do this?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This was solved, what was happening is that there was a circular dependency between the router, the models and the main file which initialized both.

Moving the Sequelize initialization logic to a different file solved the issue.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda