Saturday 15 April 2017

Exporting object looses prototyped function

I am trying to make my server.js file smaller...so I broke out the multer config into it's own file. But I get TypeError: upload.single is not a function on the upload object I exported.

This was working when I had the whole multer code in server.js, the problem only came when I broke it out into it's own file and exported the object

multer-storage.js

const multer = require('multer');

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    const path = `listing-pics/${req.body.internalUserID}`;
    mkdirp.sync(path, { opts: { mode: 0755 } } );
    cb(null, path);
  },
  filename: function (req, file, cb) {
    const fileType = file.mimetype.split('/')[1];
    const internalUserID = req.body.internalUserID;
    const filename =
      `${file.fieldname}-${internalUserID}-${Date.now()}.${fileType}`;
    cb(null, filename);
  },
  limits: { fieldSize: 25 * 1024 * 1024 }
});

const upload = multer({ storage: storage });

module.exports = {
  upload: upload
} 


server.js:

const upload = require('./app/data/multer-storage');

api.route('/listings/:listingID/images')
  .post(upload.single('image'), apiLogic.listingImages);



via dman

No comments:

Post a Comment