我面临的脚本:
router.post('/post/add', async (req, res) => {
try {
message = [];
var today = new Date();
var current_date_time = today.getFullYear() + "-" + d2(parseInt(today.getMonth() + 1)) + "-" + d2(today.getDate()) + " " + d2(today.getHours()) + ":" + d2(today.getMinutes()) + ":" + d2(today.getSeconds());
var files = req.body.files;
if (!Array.isArray(files)) files = [files];
var filename = '';
if (files.length > 0) {
for (let i = 0; i < files.length; i++) {
filename += (filename.length > 0 ? ',' : '') + (await upload_img(files[i], 'post-images'));
}
}
var tagwithids = '';
if (req.body.tagwith != null) {
tagwithids = JSON.stringify(req.body.tagwith);
}
db.query("insert into post(`text`,`files`,`status`,`member_code`,`member_type`,`group_id`,`created_date`,`post_category`,`tagwith`) values (?,?,?,?,?,?,?,?,?)", [req.body.text, filename, req.body.status, req.body.member_code, req.body.member_type, req.body.group_id, current_date_time, req.body.post_category, tagwithids], function (err, result, fields) {
db.query("SELECT MAX(post_id) as post_id FROM post where member_code='" + req.body.member_code + "'", function (err, result, fields) {
if (result != null) {
var post_id = result[0].post_id;
db.query("SELECT user_id FROM follow where member_code=?", [req.body.member_code], function (err, result, fields) {
Object.keys(result).forEach(function (key) {
var row = result[key];
db.query("insert into user_notification(`post_id`,`user_id`,`desc`,`member_code`) values(?,?,'post',?)", [post_id, row.user_id, req.body.member_code], function (err, result, fields) {
});
});
});
}
});
message.push({ msg: 'Post added Successfully' });
if (err) {
res.send(err);
}
res.json(message);
});
} catch (error) {
console.error(error);
res.status(500).send(`Unable to make post. ${error.message}`);
}
});
这不是我写的。我试图追踪为什么当用户尝试发布没有图像或视频的帖子时会引发错误。错误显示Unable to make post. Must provide one file
。查询似乎正常。