我正在使用 React
, mongoose
, node
, express
, multer
. 我想在博客内容中添加任意数量的段落或图片。我完全迷路了,我不知道该如何定义模式并将它们保存到数据库中。而且,我使用 multer.any()
作为中间件。我不太确定是否 multer
将影响非图像数据
现在让我试着只上传一个图片和一个段落,我使用console.log检查结果。
const BlogSchema = new mongoose.Schema({
title: {
type: String,
},
coverImage: {
type: String,
},
blogImage: {
type: String,
},
lastModified: {
type: String,
},
category: {
type: String,
},
blogContent: {
type: Schema.Types.Mixed, // this is the content where I can insert images and paragraphs
}
})
路线
const upload = multer({
storage: multer.memoryStorage(),
limits: {
fileSize: 5 * 1024 * 1024,
},
});
router.patch(/:id, upload.any(), (req, res, next) => {
console.log(req.body.blogContent); // Output --> [object Object],[object, Object]
console.log(req.files); // Output --> []
})
server.js
require("dotenv").config({path: "./config.env"});
const express = require("express");
const connectDB = require('./config/db');
const app = express();
// Connect DB
connectDB();
// express middleware
app.use(express.json());
app.use('/blog', require('./routes/blogPage'));
app.use(express.urlencoded({ extended: false }));
const PORT = process.env.PORT || 5000;
const server = app.listen(PORT, () => console.log(`Server running on port ${PORT}`))
// handle error
process.on("unhandledRejection", (err, promise) => {
console.log(`Logged Error: ${err}` )
server.close(() => process.exit(1))
})
我从中得到一个奇怪的输出 req.body.blogContent
并且没有从中获得任何输出 req.files
即使它应该在那里。我真的需要帮助!
暂无答案!
目前还没有任何答案,快来回答吧!