在运行express.js应用程序时消除问题



我正在尝试使用Express、Sequelize和MySQL运行一个简单的应用程序我得到这个错误:

Executing (default): CREATE TABLE IF NOT EXISTS `tasks` (`id` INTEGER NOT NULL auto_increment , `title` VARCHAR(255) NOT NULL, `description` VARCHAR(255) NOT NULL, `status` NUMBER NOT NULL, `createdBy` NUMBER, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
err:  Error
at Query.run (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulessequelizelibdialectsmysqlquery.js:52:25)
at C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulessequelizelibsequelize.js:313:28
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async MySQLQueryInterface.createTable (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulessequelizelibdialectsabstractquery-interface.js:94:12)   
at async Function.sync (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulessequelizelibmodel.js:939:5)
at async Sequelize.sync (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulessequelizelibsequelize.js:377:9) {
name: 'SequelizeDatabaseError',
parent: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NUMBER NOT NULL, `createdBy` NUMBER, `createdAt` DATETIME NOT NULL, `updatedAt` ' at line 1
at Packet.asError (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulesmysql2libpacketspacket.js:728:17)
at Query.execute (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulesmysql2libcommandscommand.js:29:26)
at Connection.handlePacket (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulesmysql2libconnection.js:456:32)
at PacketParser.onPacket (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulesmysql2libconnection.js:85:12)
at PacketParser.executeStart (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulesmysql2libpacket_parser.js:75:16)
at Socket.<anonymous> (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulesmysql2libconnection.js:92:25)
at Socket.emit (events.js:375:28)
at addChunk (internal/streams/readable.js:290:12)
at readableAddChunk (internal/streams/readable.js:265:9)
at Socket.Readable.push (internal/streams/readable.js:204:10) {
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NUMBER NOT NULL, `createdBy` NUMBER, `createdAt` DATETIME NOT NULL, `updatedAt` ' at line 1",
sql: 'CREATE TABLE IF NOT EXISTS `tasks` (`id` INTEGER NOT NULL auto_increment , `title` VARCHAR(255) NOT NULL, `description` VARCHAR(255) NOT NULL, `status` NUMBER NOT NULL, `createdBy` NUMBER, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY 
(`id`)) ENGINE=InnoDB;',
parameters: undefined
},
original: Error: You have an error in your SQL syntax; check the manual that corresponds 
to your MySQL server version for the right syntax to use near 'NUMBER NOT NULL, `createdBy` NUMBER, `createdAt` DATETIME NOT NULL, `updatedAt` ' at line 1
at Packet.asError (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulesmysql2libpacketspacket.js:728:17)
at Query.execute (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulesmysql2libcommandscommand.js:29:26)
at Connection.handlePacket (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulesmysql2libconnection.js:456:32)
at PacketParser.onPacket (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulesmysql2libconnection.js:85:12)
at PacketParser.executeStart (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulesmysql2libpacket_parser.js:75:16)
at Socket.<anonymous> (C:UsersKryolos.Hakeemdesktopvodafone-tech-testback-end-v2node_modulesmysql2libconnection.js:92:25)
at Socket.emit (events.js:375:28)
at addChunk (internal/streams/readable.js:290:12)
at readableAddChunk (internal/streams/readable.js:265:9)
at Socket.Readable.push (internal/streams/readable.js:204:10) {
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NUMBER NOT NULL, `createdBy` NUMBER, `createdAt` DATETIME NOT NULL, `updatedAt` ' at line 1",
sql: 'CREATE TABLE IF NOT EXISTS `tasks` (`id` INTEGER NOT NULL auto_increment , `title` VARCHAR(255) NOT NULL, `description` VARCHAR(255) NOT NULL, `status` NUMBER NOT NULL, `createdBy` NUMBER, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY 
(`id`)) ENGINE=InnoDB;',
parameters: undefined
},
sql: 'CREATE TABLE IF NOT EXISTS `tasks` (`id` INTEGER NOT NULL auto_increment , `title` VARCHAR(255) NOT NULL, `description` VARCHAR(255) NOT NULL, `status` NUMBER NOT NULL, `createdBy` NUMBER, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;',
parameters: {}
}
[nodemon] clean exit - waiting for changes before restart

这是我的联系:

const { Sequelize } = require('sequelize');
require('dotenv').config()
const Sequelized = new Sequelize(
process.env.DATABASE_NAME,
process.env.USER_NAME,
process.env.PASSWORD,
{
dialect: 'mysql',
host: process.env.DATABASE_HOST
}
);
module.exports = Sequelized; 

这是我的应用程序的入口

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const router = require('./routes/tasks');
const Sequelized = require('./utiles/database');
const app = express();
require('dotenv').config();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(router);
Sequelized.sync()
.then((result) => {
console.log('result: ', result);
app.listen(3001, () => console.log('App is Listining on port 3001'));
})
.catch((err) => {
console.log('err: ', err);
});

这是我的任务模型定义

const { DataTypes } = require('sequelize');
const Sequelized = require('../utiles/database');
const Task = Sequelized.define('task', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
description: {
type: DataTypes.STRING,
allowNull: false,
},
status: {
type: DataTypes.NUMBER,
allowNull: false,
},
createdBy: {
type: DataTypes.NUMBER,
allowNull: true
}
});
module.exports = Task;

这是我运行的尝试

const Task = require("../models/task");
exports.CreateTask = (req, res) => {
const { title, description, userId } = req.body;
Task.CreateTask({
title,
description,
createdBy: userId,
status: 1
}).then(res => {
console.log('res: ', res)
}).catch((err) => {
console.log('err: ', err);
})
}

这个问题只发生在我尝试使用Task开始创建任务的时候,我认为这个问题是可以避免的,任何帮助都是值得肯定的,感谢

问题是您定义了模型Taks,而在最后一段代码中,您导出了一个名为CreateTask的函数。

但是在这个箭头函数中,您试图创建一个新的Task,但是您再次调用CreateTask。您还错过了为titledescription指定要分配这些值的键

您应该这样使用Task.creat(...

const Task = require("../models/task");
exports.CreateTask = (req, res) => {
const { title, description, userId } = req.body;
Task.create({
title: title,
description: description,
createdBy: userId,
status: 1
}).then(res => {
console.log('res: ', res)
}).catch((err) => {
console.log('err: ', err);
})
}