Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

node.js - fs.readfilesync(...) gives internal server error 500

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...