I am studying MVC and using mysql and sequelize to seed database my seeds folder has index.js and users.js this is the code in seeds/index.js:
const users = require('./users');
const { User } = require('./../models');
// create seeder function
const seeder = async () => {
// this will wipe out the user table
await User.destroy({ where: {}});
// this will insert all users at one
await User.bulkCreate(users);
process.exit(0); // stop the current node process that is running and exit, 0 mean no error happened, 1 mean an error happened
};
(async () => {
await seeder ();
})();
this is seeds/users.js
module.exports = [
{
username: "captain fox 1",
password: 'password'
},
{
username: "captain fox 2",
password: 'password'
},
{
username: "captain fox 3",
password: 'password'
},
];
this is the code in models/User.js:
const { Models, DataTypes } = require('sequelize');
const sequelize = require('./../config/connection');
class User extends Models { }
User.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
},
username: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notNull: true,
}
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notNull: true,
len: [6],
},
},
},
{
sequelize,
modelName: 'users',
}
);
module.exports = User;
this is the code in my server.js:
// Dependencies
const express = require('express');
const exphbs = require('express-handlebars');
const routes = require ('./controllers/homepageController');
// const path = require('path');
// Setting extra configuration that we might want to add for handlebars
const hbs = exphbs.create({});
// Set up Express App
const app = express();
const PORT = process.env.PORT || 3001;
// Setting up a basic express handlebars app by using the configuration when we declared "const hbs = exphbs.create({});"
app.engine('handlebars', hbs.engine);
// Express when we call res.render, we mean to use a handlebars template
app.set('view engine', 'handlebars');
// middlewares
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(routes);
// app.use(express.static(path.join(__dirname,'public')));
// app.use(require('.controllers/'))
// Start the server to begin listening
app.listen(PORT, () => {
console.log('Server listening on : http://localhost:' PORT);
});
this is my models/index.js:
const User = require('./User');
module.exports = {
User,
};
when I run node seeds in my command line inside root folder it's showing this error:
C:\Users\blablabla\Documents\bootcamp\playground\practice\MVC_todos\models\User.js:6
class User extends Models { }
^
TypeError: Class extends value undefined is not a constructor or null
at Object. (C:\Users\blablabla\Documents\bootcamp\playground\practice\MVC_todos\models\User.js:6:20)
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. (C:\Users\blablabla\Documents\bootcamp\playground\practice\MVC_todos\models\index.js:1:14)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
trtkh@DESKTOP-8T4104A MINGW64 ~/Documents/bootcamp/playground/practice/MVC_todos (main)
$ node seeds
C:\Users\blablabla\Documents\bootcamp\playground\practice\MVC_todos\models\User.js:6
class User extends Models { }
^
TypeError: Class extends value undefined is not a constructor or null
at Object. (C:\Users\blablabla\Documents\bootcamp\playground\practice\MVC_todos\models\User.js:6:20)
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. (C:\Users\blablabla\Documents\bootcamp\playground\practice\MVC_todos\models\index.js:1:14)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
please help and explain what had happened !!! Thank you very much!!!
CodePudding user response:
I think that you should extend Model
class, not Models
:
const { Model, DataTypes } = require('sequelize'); //Model instead of Models
const sequelize = require('./../config/connection');
class User extends Model { } //Model instead of Models
User.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
},
username: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notNull: true,
}
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notNull: true,
len: [6],
},
},
},
{
sequelize,
modelName: 'users',
}
);
module.exports = User;
CodePudding user response:
I think you have a problem with your Models import, check that out. Maybe you need to import just Model without the 's'.