Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't Import generated models #43

Open
estellederrien opened this issue Nov 2, 2020 · 0 comments
Open

Can't Import generated models #43

estellederrien opened this issue Nov 2, 2020 · 0 comments

Comments

@estellederrien
Copy link

estellederrien commented Nov 2, 2020

Hello,

I can't import the generated models, this is my code 馃憤

const db = {}
const __dirname = "models"
fs
    .readdirSync(__dirname)
    .forEach((file) => {
        var a = path.join(__dirname, file);
        const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes)
        db[model.name] = model;
    })
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;

The error 馃憤

Unable to connect to the database: { Error: Cannot find module 'models\customers.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at fs.readdirSync.forEach (C:\uwamp\www\vue-node-mysql-generated\server.js:138:35)
    at Array.forEach (<anonymous>)
    at sequelize.authenticate.then (C:\uwamp\www\vue-node-mysql-generated\server.js:136:18)
    at process._tickCallback (internal/process/next_tick.js:68:7) code: 'MODULE_NOT_FOUND' }

Can somebody give me the right code ?

This is the generated customers.js

const {
    DataTypes
} = require('sequelize');

module.exports = sequelize => {
    const attributes = {
        customerNumber: {
            type: DataTypes.INTEGER,
            allowNull: false,
            defaultValue: null,
            primaryKey: true,
            autoIncrement: false,
            comment: null,
            field: "customerNumber"
        },
        customerName: {
            type: DataTypes.STRING(50),
            allowNull: false,
            defaultValue: null,
            primaryKey: false,
            autoIncrement: false,
            comment: null,
            field: "customerName"
        };
};
    const options = {
        tableName: "customers",
        comment: "",
        indexes: [{
            name: "salesRepEmployeeNumber",
            unique: false,
            type: "BTREE",
            fields: ["salesRepEmployeeNumber"]
        }]
    };
    const CustomersModel = sequelize.define("customers_model", attributes, options);
    return CustomersModel;
};

This other command is not working as well ;
import CustomersModel from '/models/customers.js'

SyntaxError: Unexpected identifier

This oher code doesnt work

import CustomersModel from 'models/customers'
            const User = CustomersModel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant