如何在生成 index.js 时修复 sequelize 连接问题?

How to fix sequelize connection issue while generating index.js?

我是 node.js 的新手,在使用 sequelize-cli 时遇到了一些问题。 我有 sequelize 模型,当我 运行 项目时,我在 index.js:

中出错
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
TypeError: require(...) is not a function

项目无法找到或读取此内容(续集,Sequelize.DataTypes)。

我的项目:server.js

const express = require("express");
const productRouter = require("./routers/productRouter");
const discountRouter = require("./routers/discountRouter")

const app = express();
const PORT = process.env.PORT || 3000;

app.use(express.json());

// Запрос на таблицу с продуктами
app.use("/shop", productRouter);
// Запрос на таблицу со скидками
app.use("/shop", discountRouter);

app.listen(PORT, () => console.log("Server is working ... "));

productRouter.js

const Router = require("express");
const router = new Router();
const productController = require("../controllers/productControllers");

// Получаем все товары
router.get("/products", async (req, res, next) => {
    const resultOfGetAllProducts = await productController.all();
    if(resultOfGetAllProducts === Error) {
        return res.sendStatus(500).json(Error);
    } else {
        return res.sendStatus(200).json(resultOfGetAllProducts);
    };
});

// Получаем конкретный товар
router.get("/product/:id", async (req, res, next) => {
    if(!req.params.id) return res.sendStatus(400).send("Product ID is not specified.");
    const id = req.params.id;
    const result = null;
    const resultOfGetOneProduct = await productController.one(id, result);
    if(resultOfGetOneProduct === Error) {
        return res.sendStatus(500).json(Error);
    } else {
        return res.sendStatus(200).json(resultOfGetOneProduct);
    };
});

// Добавляем товар
router.post("/product", async (req, res, next) => {
    if(!req.body) return res.sendStatus(400).send("Product parameters are not specified.");
    const product = {
        product_name: req.body.product_name,
        price: req.body.price,
        product_description: req.body.product_description
    };
    const result = null;
    const resultOfCreateProduct = await productController.create(product, result);
    if (resultOfCreateProduct === Error) {
        return res.sendStatus(500).json(Error);
    } else {
        return res.sendStatus(200).send("The product has been created.");
    }
});

// Обновляем товар
router.put("/product/:id", async (req, res, next) => {
    if(!req.params.id) return res.sendStatus(400).send("Product ID is not specified.");
    if(!req.body) return res.sendStatus(400).send("Product parameters are not specified.");
    const product = {
        product_name: req.body.product_name,
        price: req.body.price,
        product_description: req.body.product_description
    }
    const id = {id: req.params.id};
    const result = null;
    const resultOfUpdateProduct = await productController.update(id, product, result);
    if(resultOfUpdateProduct === Error) {
        return res.sendStatus(500).json(Error);
    } else {
        return res.sendStatus(200).send("The product has been updated.");
    };
});

// Удаляем товар
router.delete("/product/:id", async (req, res, next) => {
    if(!req.params.id) return res.sendStatus(400).send("Product ID is not specified.");
    const id = {id: req.params.id};
    const result = null;
    const resultOfDeleteProduct = await productController.delete(id, result);
    if(resultOfDeleteProduct === Error) {
        return res.sendStatus(500).json(Error);
    } else {
        return res.sendStatus(200).send("The product has been deleted.");
    }
});


module.exports = router;

productControllers.js

const productModels = require("../models/productModels");

exports.all = async function getAllProducts() {
    const result = await productModels.all(function(err, docs) {
        if (err) {
            console.log(err);
            return err;
        } else {
            return docs;
        };
    });
    return result;
};

exports.one = async function getOneProduct(id, cb) {
    const result = await productModels.one(id, function(err, doc) {
        if (err) {
            console.log(err);
            return err;
        } else {
            return doc; 
        }
    });
    cb = result;
    return cb;
};

exports.create = async function createProduct(product, cb) {
    const confirmationOfCreate = await productModels.create(product, function(err, result) {
        if (err) {
            console.log(err);
            return err;
        } else {
            return result;
        }
    });
    cb = confirmationOfCreate;
    return cb;
};

exports.update = async function updateProduct(id, product, cb) {
    const confirmationOfUpdate = await productModels.update(id, product, function(err, result) {
        if (err) {
            console.log(err);
            return err;
        } else {
            return result;
        }
    });
    cb = confirmationOfUpdate;
    return cb;
};

exports.delete = async function deleteProduct(id, cb) {
    const confirmationOfDelete = await productModels.delete(id, function(err, result) {
        if (err) {
            console.log(err);
            return err;
        } else {
            return result;
        }
    });
    cb = confirmationOfDelete;
    return cb;
};

productModels.js

const Sequelize = require("sequelize");
const productsModel = require("./products.js");
const db = require("./index.js");

// Получаю с бд все продукты
exports.all = async function getProducts(cb) {
    //const products = await db.query('SELECT * FROM products');
    const products = await db.findAll({raw: true});
    if(products === null) {
        return cb(Error, null);
    } else {
        return cb(null, products);
    };
};

// Получаю с бд конкретный продукт
exports.one = async function getOneProduct(id, cb) {
    //const getOneQuery = 'SELECT * FROM product where id = ';
    //const getDiscount = 'SELECT * FROM discounts where product_id = ';
    //const curDiscount = await Discount.findOne({where: id});
    const product = await db.findAll({where: {id: id}});
    const productDiscount = await Discount.findAll({where: {product_id: id}});
    const priceWithDiscount = product.price - (product.price * ((productDiscount.discount)/100));
    if (product === null) {
        return cb(Error, null);
    } else if(productDiscount === null) {
        return cb(null, product)
    } else {
        product.price = priceWithDiscount;
        const result = product;
        return cb(null, result);
    };
};

// Создаю в бд продукт
exports.create = async function createProduct(product, cb) {
    // const createQuery = 'INSERT INTO product (product_name, price, product_description) values (, , ) RETURNING *';
    // const arrayQuery = [product.product_name, product.price, product.product_description];
    // const newProduct = await db.query(createQuery, arrayQuery);    
    const newProduct = await db.create(product);
    if(newProduct === null) {
        return cb(Error, null);
    } else {
        return cb(null, newProduct);
    };
};

// Обновляю в бд конкретный продукт
exports.update = async function updateProduct(id, newData, cb) {
    // const updateQuery = 'UPDATE product set product_name = , price = , product_description =  where id =  RETURNING *';
    // const arrayQuery = [newData.product_name, newData.price, newData.product_description, id];
    const product = await db.update(newData, {where: id});
    if(product === null) {
        return cb(Error, null);
    } else {
        return cb(null, product);
    };
};

// Удаляю в бд конкретный продукт
exports.delete = async function deleteProduct(id, cb) {
    //const deleteQuery = 'DELETE FROM product where id = ';
    const product = await db.destroy({where: id});
    if(product === null) {
        return cb(Error, null);
    } else {
        return cb(null, product);
    };
};

product.js

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class products extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  };
  products.init({
    product_name: DataTypes.STRING,
    price: DataTypes.NUMBER,
    product_description: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'products',
  });
  return products;
};

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.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 = 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;

我还有迁移文件和配置文件。

我必须做什么才能发出此错误或其他问题?

移动 productModels.js 到 'repository' 目录 因为,此文件中没有 'default' 导出

并且,数据库操作文件不能位于模型文件夹中

我解决了这个问题,并上传到 git https://github.com/nkhs/node-sq-stack.git