I want to store a PDF file in MongoDB and retrieve it using react and express. But in the express file when I am using the fs.readfilesync(...)
method, it throws 500 (Internal Server Error)
.
React file:
const fd = new FormData();
fd.append("file", e.target.files[0]);
fd.set("data", e.target.title.value);
axios
.post(url, fd)
.then(...)
.catch(...);
Mongoose schema:
const mongoose = require('mongoose');
const fileSchema = new mongoose.Schema({
title: String,
pdf: {
data: Buffer,
contentType: String
}
})
module.exports = new mongoose.model('File', fileSchema);
Express file:
const AddFile = require('../models/add_file');
const newFile = new AddFile({
title: req.body.data,
pdf: {
data: fs.readFileSync(path.join(__dirname + '/public/files/' + req.file.filename)), //this line throws error
contentType: 'application/pdf'
}
});
newFile.save(...);
question from:
https://stackoverflow.com/questions/65651533/fs-readfilesync-gives-internal-server-error-500 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…