1
resposta

Não consigo identificar o problema

Ao iniciar o servidor, aparece este erro no terminal

C:\Users\Denise\Alura Node JS\orm-com-node\api\models\index.js:24
    const model = sequelize['import'](path.join(__dirname, file));
                                     ^

TypeError: sequelize.import is not a function
    at C:\Users\Denise\Alura Node JS\orm-com-node\api\models\index.js:24:38
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (C:\Users\Denise\Alura Node JS\orm-com-node\api\models\index.js:23:4)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Users\Denise\Alura Node JS\orm-com-node\api\controllers\PessoaController.js:1:18)
[nodemon] app crashed - waiting for file changes before starting...

O arquivo index da pasta models está assim

'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    const model = sequelize['import'](path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
1 resposta

Oi Matheus, tudo bem? Você tá usando alguma versão diferente do Sequelize que a mostrada no curso? Pergunto isso por que o import de model como tá no código: sequelize['import'](path.join(__dirname, file)) foi descontinuado pelo Sequelize.