Node.js Multer “.array is not a function”

84
January 10, 2020, at 6:00 PM

I've been looking and trying to figure this out for two days now, and the only real mention of it that I can find is an old issue report on version 1.1.0 which was closed without resolution: https://github.com/expressjs/multer/issues/338

I'm using Node.js SDK and Express framework, with Cloudinary as my image host.

This is just a project for a class, but it's important that I know how to get this to work for future projects.

const multer = require('multer');
//configure where/how files are stored temporarily
const storage = multer.diskStorage({
    filename: function(req,file,cb) {
        cv(null,Date.now() + file.originalname);
    },
    destination: function (req, file, cb) {
        cb(null, '/uploads')
      }
});
//only accept image files for cloudinary
const imageFilter = (req,file,cb) => {
    if (!file.originalname.match(/\.jpg|jpeg|png|gif)$/i)) {
        return cb(new Error('Only image files (jpg, jpeg, png, gif) are allowed!'), false);
    }
    else {
        cb(null,true);
    }
};
//configure multer's upload parameters
const upload = multer({storage:storage, filefilter:imageFilter}).array('image',5);
/* POST new post  */
router.post('/', upload.array('image', 5) ,asyncErrorHandler(postCreate));

console error when trying to run Node: TypeError: upload.array is not a function

I'm handling the actual logic of using the files in what is supposed to be the "files" array, but it won't even get that far, it just stops at upload.array() when trying to compile the file.

Below are the dependencies from my package.json just in case any other modules are known to interfere:

  "dependencies": {
    "async": "^3.1.0",
    "body-parser": "^1.19.0",
    "cloudinary": "^1.18.1",
    "connect-flash": "^0.1.1",
    "cookie-parser": "~1.4.4",
    "ejs": "^3.0.1",
    "express": "~4.16.1",
    "express-sanitizer": "^1.0.5",
    "express-session": "^1.17.0",
    "http-errors": "~1.6.3",
    "locus": "^2.0.4",
    "method-override": "^3.0.0",
    "mongoose": "^5.8.5",
    "morgan": "~1.9.1",
    "multer": "^1.4.2",
    "passport": "^0.4.1",
    "passport-local": "^1.0.0",
    "passport-local-mongoose": "^6.0.1",
    "serve-favicon": "^2.5.0"
  }
Answer 1

Well, I found my problem:

I was calling the array method both when creating the upload constant as well as in the middleware for the route. Removed it from the constant declaration and all is well.

Now if only I could find out why I can't assign any of the cloudinary uploader response values to an object variable...

READ ALSO
Sending multiple files in nodejs through TCP sockets

Sending multiple files in nodejs through TCP sockets

Here is the logical that im using:

79
store dispatch actions is not a Constructor in Angular

store dispatch actions is not a Constructor in Angular

I am using ngrx for storing dataI am getting below error while calling action?

116
Sort array with a search keyword - Javascript

Sort array with a search keyword - Javascript

I need to sort a javascript array within the search result with the keyword which we searchedFor example:

100
Rails 6 JavaScripts

Rails 6 JavaScripts

I'm trying to implement some JavaScript in my Rails 6 app, I have no idea if I'm going about it the right way but so far everything I've tried has been wrong

115