开发者问题收集

在 MVC 家庭作业中运行节点种子时,TypeError:类扩展值未定义不是构造函数或为空

2022-08-25
607

我正在学习 MVC,并使用 mysql 和 sequelize 来播种数据库 我的 seeds 文件夹有 index.js 和 users.js 这是 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 ();
})();

这是 seeds/users.js

module.exports = [
{
username: "captain fox 1",
password: 'password'
},
{
username: "captain fox 2",
password: 'password'
},
{
username: "captain fox 3",
password: 'password'
},

];

这是 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;

这是我的 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);
});

这是我的 models/index.js:

const User = require('./User');


module.exports = {
    User,
};

当我在根文件夹内的命令行中运行 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)

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)

请帮忙解释发生了什么!!! 非常感谢!!!

2个回答

我认为您应该扩展 Model 类,而不是 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;
Alon Barenboim
2022-08-25

我认为您的模型导入存在问题,请检查一下。也许您需要导入不带“s”的模型。

Robi Szekely
2022-08-25