Sequelize 创建我的数据库但不将文件发送到该数据库
Sequelize create my database but doesn't send files into that
我正在尝试使用 express 和 sequelize 创建后端,我已经配置了配置、控制器、模型、路由和服务器文件,对于第一次测试,数据库使用两个 tables 成功创建(产品和评论 tables)现在我正在尝试测试使用 sequelize 创建的模型和控制器,但是当我尝试使用 post 路由发送产品内部的信息时 table:
const db = require('../models');
// create main model
const Product = db.products;
const Review = db.reviews;
// main work
// 1. create product
const addProduct = async (req, res) => {
let info = {
title: req.body.title,
price: req.body.price,
description: req.body.description,
published: req.body.published ? req.body.published : false
}
const product = await Product.create(info)
res.status(200).send(product)
console.log(product)
}
module.exports = {
addProduct,
}\
,从控制器文件夹,从数据库和模型文件夹获取信息:
module.exports = (sequelize, DataTypes) => {
const Product = sequelize.define("product", {
title: {
type: DataTypes.TEXT,
allowNull: false
},
price: {
type: DataTypes.FLOAT,
allowNull: false
},
description: {
type: DataTypes.TEXT
},
published: {
type: DataTypes.BOOLEAN
},
});
return Product;
}
我在终端上收到一条错误消息:
nodejs\node-fullApi\controllers\productController.js:13
title: req.body.title,
^
TypeError: Cannot read properties of undefined (reading 'title')
at addProduct (F:\JavaScript\nodejs\node-fullApi\controllers\productController.js:13:21)
at Layer.handle [as handle_request] (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\layer.js:95:5)
at next (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\layer.js:95:5)
at F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\index.js:281:22
at Function.process_params (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\index.js:341:12)
at next (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\index.js:275:10)
at Function.handle (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\index.js:174:3)
at router (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\index.js:47:12)
[nodemon] app crashed - waiting for file changes before starting...
我试图回顾我所做的一切,我将标题的数据类型从 STRING 更改为 TEXT,但没有任何效果,而且我不断收到同样的错误。
有谁知道哪里出了什么问题吗?
我的完整项目可以在 github 上找到 link:My Git Repository
您需要注册所有主体解析器BEFORE注册任何可能使用给定格式的请求主体的路由器:
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const router = require('../routes/productRouter');
app.use('/api/products', router)
我正在尝试使用 express 和 sequelize 创建后端,我已经配置了配置、控制器、模型、路由和服务器文件,对于第一次测试,数据库使用两个 tables 成功创建(产品和评论 tables)现在我正在尝试测试使用 sequelize 创建的模型和控制器,但是当我尝试使用 post 路由发送产品内部的信息时 table:
const db = require('../models');
// create main model
const Product = db.products;
const Review = db.reviews;
// main work
// 1. create product
const addProduct = async (req, res) => {
let info = {
title: req.body.title,
price: req.body.price,
description: req.body.description,
published: req.body.published ? req.body.published : false
}
const product = await Product.create(info)
res.status(200).send(product)
console.log(product)
}
module.exports = {
addProduct,
}\
,从控制器文件夹,从数据库和模型文件夹获取信息:
module.exports = (sequelize, DataTypes) => {
const Product = sequelize.define("product", {
title: {
type: DataTypes.TEXT,
allowNull: false
},
price: {
type: DataTypes.FLOAT,
allowNull: false
},
description: {
type: DataTypes.TEXT
},
published: {
type: DataTypes.BOOLEAN
},
});
return Product;
}
我在终端上收到一条错误消息:
nodejs\node-fullApi\controllers\productController.js:13
title: req.body.title,
^
TypeError: Cannot read properties of undefined (reading 'title')
at addProduct (F:\JavaScript\nodejs\node-fullApi\controllers\productController.js:13:21)
at Layer.handle [as handle_request] (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\layer.js:95:5)
at next (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\layer.js:95:5)
at F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\index.js:281:22
at Function.process_params (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\index.js:341:12)
at next (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\index.js:275:10)
at Function.handle (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\index.js:174:3)
at router (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\index.js:47:12)
[nodemon] app crashed - waiting for file changes before starting...
我试图回顾我所做的一切,我将标题的数据类型从 STRING 更改为 TEXT,但没有任何效果,而且我不断收到同样的错误。
有谁知道哪里出了什么问题吗? 我的完整项目可以在 github 上找到 link:My Git Repository
您需要注册所有主体解析器BEFORE注册任何可能使用给定格式的请求主体的路由器:
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const router = require('../routes/productRouter');
app.use('/api/products', router)