javascript - 类型错误:无法读取未定义的属性“名称”|| 文件上传问题
问题描述
我无法添加帖子,我认为问题与文件上传有关。
这是错误:
TypeError: Cannot read property 'name' of undefined
at addPost (C:\Users\Web-Developer\Desktop\crud-app\routes\post.js:21:37)
at Layer.handle [as handle_request] (C:\Users\Web-Developer\Desktop\crud-app\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Web-Developer\Desktop\crud-app\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Web-Developer\Desktop\crud-app\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Web-Developer\Desktop\crud-app\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Web-Developer\Desktop\crud-app\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Web-Developer\Desktop\crud-app\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Web-Developer\Desktop\crud-app\node_modules\express\lib\router\index.js:275:10)
at C:\Users\Web-Developer\Desktop\crud-app\node_modules\express-fileupload\lib\processMultipart.js:140:9
at processTicksAndRejections (internal/process/task_queues.js:97:5)
这是我的代码:
addPost: (req, res) => {
if (!req.files) {
return res.status(400).send("No files were uploaded.");
}
let message = '';
let title = req.body.title;
let description = req.body.description;
let category = req.body.category;
let date = req.body.date;
let main_image = req.files.main_image;
let image_name = main_image.name;
let fileExtension = main_image.mimetype.split('/')[1];
image_name = title + '.' + fileExtension;
let titleQuery = "SELECT * FROM `posts` WHERE title = '" + title + "'";
db.query(titleQuery, (err, result) => {
if (err) {
return res.status(500).send(err);
}
if (result.length > 0) {
message = 'Post already exists';
res.render('add-player.ejs', {
message,
title: 'Welcome to BLOG'
});
} else {
// check the filetype before uploading it
if (main_image.mimetype === 'image/png' || main_image.mimetype === 'image/jpeg' || main_image.mimetype === 'image/gif') {
// upload the file to the /public/assets/img directory
main_image.mv(`public/assets/img/${image_name}`, (err ) => {
if (err) {
return res.status(500).send(err);
}
// send the post's details to the database
let query = "INSERT INTO `posts` (title, description, category, date, main_image) VALUES ('" +
title + "', '" + description + "', '" + category + "', '" + date + "', '" + image_name + "')";
db.query(query, (err, result) => {
if (err) {
return res.status(500).send(err);
}
res.redirect('/');
});
});
} else {
message = "Invalid File format. Only 'gif', 'jpeg' and 'png' images are allowed.";
res.render('add-player.ejs', {
message,
title: 'Welcome to BLOG'
});
}
}
});
}
解决方案
推荐阅读
- c++ - How to solve Undefined symbols for architecture x86_64 error when using C++ API `llvm::InitializeAllAsmParsers()` in LLVM?
- python - How to fill 2d numpy array with some 2d numpy array
- node.js - I still get the MongooseServerSelectionError after allowing all access from anywhere when i connect with heroku
- azure - 如何让我的 AKS 群集向我的 ACR 进行身份验证?
- python-asyncio - 来自 aiohttp.ClientSession().post() 的带有多个图像文件的格式错误的请求
- java - Eclipse IDE 项目资源管理器窗口中的奇怪字符
- c++ - 在 Linux 终端中使用 `std::wstring` 和 `std::wcout` 打印拉丁字符
- twitter-bootstrap - Bootstrap 4 布局设计
- android - Android Studio 无法解决:.espresso:espresso-core:3.2.0
- excel - 将 300 张数据表添加到 Excel 的快速方法是什么?