Node.js Multer “.array 不是函数”

Node.js Multer ".array is not a function"

两天来我一直在寻找并试图解决这个问题,我能找到的唯一真正提到它的是关于 1.1.0 版的旧问题报告,该报告在没有解决的情况下关闭:https://github.com/expressjs/multer/issues/338

我正在使用 Node.js SDK 和 Express 框架,并将 Cloudinary 作为我的图像宿主。

这只是一个 class 的项目,但重要的是我知道如何让它在未来的项目中发挥作用。

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));

尝试 运行 节点时出现控制台错误:TypeError: upload.array is not a function

我正在处理使用应该是 "files" 数组中的文件的实际逻辑,但它甚至不会走那么远,它只是停在 upload.array() 时正在尝试编译文件。

以下是我的 package.json 的依赖项,以防已知任何其他模块干扰:

  "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"
  }

嗯,我发现了我的问题:

我在创建 upload 常量时以及在为路由调用该常量时在中间件中调用了 array() 方法。将其从常量声明中删除,一切正常。