简体   繁体   中英

Why does sequelize not recognize the Model? TypeError: Cannot read properties of undefined (reading 'findAll')

So I'm learning sequelize with MySQL and I have a few examples at my disposal on how it should be done, but this error persists as if sequelize couldn't use the functions inside the model. I just can't find why it's not working, any help?

This is the error that shows in the terminal, it points to productController line 8: TypeError: Cannot read properties of undefined (reading 'findAll')

This is my Index.js:

'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.js')[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 = require(path.join(__dirname, file));(sequelize, Sequelize.DataTypes);
    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;

This is the Table Model:

module.exports = (sequelize, DataTypes) => {
    let alias = 'Product';
    let atributes = {
        id: { 
            type: DataTypes.INTEGER,
            primaryKey: true,
            allowNull: false,
            autoIncrement: true
        },
        name: { 
            type: DataTypes.STRING,
            allowNull: false,
        },
    }
    let config = {
        tableName: 'products',
        timestamps: false
    }

    const Product = sequelize.define(alias, atributes, config);

    return Product
}

And This productController.js:

const db = require('../../models/Index');
const sequelize = db.sequelize;

const productsController = {
    'shopRender' : (req, res) => {
        // res.send('hola mundo');
        db.Product.findAll()
            .then(function(productos) {
                res.send("hola mundo");
            })
    }
}

module.exports = productsController;

Ive tried deconstructing db in productController into Product, using caps, non-caps, using the table name...

What does work is trying to show something inside the shopRender function, but once I try to use the model to do a query and try to access the route, the error appears.

Turns out, trying to use res.send(Object.keys(db)) to debug prints ["undefined","","sequelize","Sequelize"]

try this on a productController.js, I hope it helps.

const db = require('../../models').sequelize.models;

const productsController = {
    'shopRender' : (req, res) => {
        // res.send('hola mundo');
        db.Product.findAll()
            .then(function(productos) {
                res.send("hola mundo");
            })
    }
}

module.exports = productsController;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM