|
@@ -99,6 +99,10 @@ router.post('/', async function(req, res, next) {
|
|
|
//白名单过滤
|
|
|
const body = filterBody(req)
|
|
|
|
|
|
+ // 添加调试日志
|
|
|
+ console.log('创建文章请求体大小:', JSON.stringify(body).length);
|
|
|
+ console.log('Content字段长度:', body.content ? body.content.length : 0);
|
|
|
+
|
|
|
const article = await Article.create(body)
|
|
|
res.status(201).json({
|
|
|
status:true,
|
|
@@ -106,6 +110,11 @@ router.post('/', async function(req, res, next) {
|
|
|
data:article
|
|
|
});
|
|
|
}catch(error){
|
|
|
+ // 添加详细的错误日志
|
|
|
+ console.error('创建文章错误:', error);
|
|
|
+ console.error('错误堆栈:', error.stack);
|
|
|
+ console.error('请求体:', JSON.stringify(req.body, null, 2));
|
|
|
+
|
|
|
if(error.name === 'SequelizeValidationError'){
|
|
|
const errors = error.errors.map(e =>e.message)
|
|
|
|
|
@@ -114,10 +123,22 @@ router.post('/', async function(req, res, next) {
|
|
|
message:'请求参数错误',
|
|
|
errors
|
|
|
});
|
|
|
+ }else if(error.name === 'SequelizeDatabaseError'){
|
|
|
+ res.status(500).json({
|
|
|
+ status:false,
|
|
|
+ message:'数据库错误',
|
|
|
+ errors:[error.message]
|
|
|
+ });
|
|
|
+ }else if(error.name === 'SequelizeConnectionError'){
|
|
|
+ res.status(500).json({
|
|
|
+ status:false,
|
|
|
+ message:'数据库连接错误',
|
|
|
+ errors:[error.message]
|
|
|
+ });
|
|
|
}else{
|
|
|
res.status(500).json({
|
|
|
status:false,
|
|
|
- message:'失败',
|
|
|
+ message:'服务器内部错误',
|
|
|
errors:[error.message]
|
|
|
});
|
|
|
}
|
|
@@ -198,18 +219,35 @@ router.put('/:id', async function(req, res, next) {
|
|
|
});
|
|
|
|
|
|
function filterBody(req){
|
|
|
- return {
|
|
|
- title:req.body.title,
|
|
|
- content:req.body.content,
|
|
|
- type:req.body.type,
|
|
|
- img:req.body.img,
|
|
|
- date:req.body.date,
|
|
|
- author:req.body.author,
|
|
|
- category:req.body.category,
|
|
|
- crop:req.body.crop,
|
|
|
- seoKeyword:req.body.seoKeyword,
|
|
|
- seoDescription:req.body.seoDescription
|
|
|
+ // 数据清理和验证
|
|
|
+ const body = {
|
|
|
+ title: req.body.title ? String(req.body.title).trim() : null,
|
|
|
+ content: req.body.content ? String(req.body.content) : null,
|
|
|
+ type: req.body.type ? parseInt(req.body.type) : null,
|
|
|
+ img: req.body.img ? String(req.body.img).trim() : null,
|
|
|
+ date: req.body.date ? new Date(req.body.date) : null,
|
|
|
+ author: req.body.author ? String(req.body.author).trim() : null,
|
|
|
+ category: req.body.category ? parseInt(req.body.category) : null,
|
|
|
+ crop: req.body.crop ? parseInt(req.body.crop) : null,
|
|
|
+ seoKeyword: req.body.seoKeyword ? String(req.body.seoKeyword).trim() : null,
|
|
|
+ seoDescription: req.body.seoDescription ? String(req.body.seoDescription).trim() : null
|
|
|
+ };
|
|
|
+
|
|
|
+ // 验证必填字段
|
|
|
+ if (!body.title) {
|
|
|
+ throw new Error('标题不能为空');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!body.content) {
|
|
|
+ throw new Error('内容不能为空');
|
|
|
}
|
|
|
+
|
|
|
+ // 验证标题长度
|
|
|
+ if (body.title.length > 255) {
|
|
|
+ throw new Error('标题长度不能超过255个字符');
|
|
|
+ }
|
|
|
+
|
|
|
+ return body;
|
|
|
}
|
|
|
|
|
|
module.exports = router;
|