1

Been stuck on this for a while now. I have a mean stack application using angular 6. I'm trying to upload save a file to my backend. I've found THIS article. But either I receive an error a crypt something like

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> 
<title>Error</title> </head> <body> <pre>Error: ENOENT: no such file or 
 directory, open ''<br> at 
 Error (native)</pre> </body> </html>

returned from the server or the req.file is empty and my save doesnt happen. I'm trying to piece together the best way to do this.

Thanks

here is my end point

app.post('/api/posts', upload.single('file'), (req, res, next) => {

const post = new Post({
  title: req.body.title,
  content: req.body.content,
  image:req.file
});    

post
.save()
.then(createdPost => {
  res.status(201).json({
    status: true,
    id: createdPost._id,
  });
})
.catch(error => {
  res.status(500).json({ status: false, message: 'Didnt save ', error });
});

});

And here is how I set up multer, grid file stream and storage

    let gfs;
conn.once('open', () => {
  gfs = Grid(conn.db, mongoose.mongo);
  gfs.collection('uploads');
});


const storage = new GridFsStorage({
  url: connection,
  file: (req, file) => {
    return new Promise((resolve, reject) => {
      crypto.randomBytes(16, (err, buf) => {
        if (err) {
          return reject(err);
        }
        const filename = buf.toString('hex') + path.extname(file.originalname);
        const fileInfo = {
          filename: filename,
          bucketName: 'uploads'
        };
        resolve(fileInfo);
      });
    });
  }
});
const upload = multer({ storage });
Troy Bryant
  • 994
  • 8
  • 29
  • 60

1 Answers1

0

It has to do with multer trying to create directory Write the proper pathname (probably ./uploads/) . and make sure that folder is writable

Joel Wembo
  • 814
  • 6
  • 10