【发布时间】:2021-04-11 22:32:39
【问题描述】:
这是我的 server.js 当我运行 node server.js 时出现错误
执行中(默认):SHOW INDEX FROM user_roles
更多代码可以看这里
https://github.com/bezkoder/node-js-jwt-auth
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
var corsOptions = {
origin: "http://localhost:8081"
};
app.use(cors(corsOptions));
// parse requests of content-type - application/json
app.use(bodyParser.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// database
const db = require("./app/models");
const Role = db.role;
db.sequelize.sync();
// force: true will drop the table if it already exists
// db.sequelize.sync({force: true}).then(() => {
// console.log('Drop and Resync Database with { force: true }');
// initial();
// });
// simple route
app.get("/", (req, res) => {
res.json({ message: "Welcome to bezkoder application." });
});
// routes
require('./app/routes/auth.routes')(app);
require('./app/routes/user.routes')(app);
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
function initial() {
Role.create({
id: 1,
name: "user"
});
Role.create({
id: 2,
name: "moderator"
});
Role.create({
id: 3,
name: "admin"
});
}
我正在使用 Sequelize 是一个基于 Promise 的 Node.js ORM,用于 Postgres、MySQL、MariaDB、SQLite 和 Microsoft SQL Server。它具有可靠的事务支持、关系、急切和延迟加载、读取复制等。
node --version
v15.5.0
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.user = require("../models/user.model.js")(sequelize, Sequelize);
db.role = require("../models/role.model.js")(sequelize, Sequelize);
db.role.belongsToMany(db.user, {
through: "user_roles",
foreignKey: "roleId",
otherKey: "userId"
});
db.user.belongsToMany(db.role, {
through: "user_roles",
foreignKey: "userId",
otherKey: "roleId"
});
db.ROLES = ["user", "admin", "moderator"];
module.exports = db;
这是我在终端中得到的输出
(节点:6384)[SEQUELIZE0004] DeprecationWarning:布尔值已传递给 options.operatorsAliases。这是 v5 的无操作,应该被删除。 (使用
node --trace-deprecation ...显示警告的创建位置)
服务器在 8080 端口上运行。
执行(默认): CREATE TABLE IF NOT EXISTSusers(idINTEGER NOT NULL auto_increment ,usernameVARCHAR(255),passwordVARCHAR(255),createdAtDATE非空,updatedAt日期时间非空,主键 (id)) ENGINE=InnoDB;
执行中(默认):SHOW INDEX FROMusers
执行(默认): CREATE TABLE IF NOT EXISTSroles(idINTEGER ,nameVARCHAR(255),createdAtDATETIME NOT NULL,updatedAtDATETIME NOT NULL, PRIMARY KEY (id)) ENGINE =InnoDB;
执行中(默认):SHOW INDEX FROMroles
执行(默认): CREATE TABLE IF NOT EXISTSuser_roles(createdAtDATETIME NOT NULL,updatedAtDATETIME NOT NULL,roleIdINTEGER ,userIdINTEGER , PRIMARY KEY (roleId, @98765434@8)外键 (roleId) 引用roles(id) 更新级联时删除级联,外键 (userId) 引用users(id) 更新级联时删除级联) 引擎=Inno;
执行中(默认):SHOW INDEX FROMuser_roles
然后挂起
【问题讨论】:
-
我没有看到错误消息本身。您可以将其添加到您的帖子中吗?
-
@Antony 我已添加
标签: node.js sequelize.js