Search code examples
herokumultergridfsstorage-engines

How do I fix Multer-Gridfs-Storage error "Error creating storage engine. At least one of url or db option must be provided"?


As I am trying to deploy my app to Heroku, I get the error "Error creating storage engine. At least one of url or db option must be provided." in my heroku logs. However, I get no errors while developing and testing the app in my IDE.

Here is my error log:

...
2020-10-17T05:24:18.106601+00:00 app[web.1]: Error: Error creating storage engine. At least one of url or db option must be provided.
2020-10-17T05:24:18.106602+00:00 app[web.1]: at new GridFSStorage (/app/node_modules/multer-gridfs-storage/lib/gridfs.js:59:10)
2020-10-17T05:24:18.106602+00:00 app[web.1]: at Object.<anonymous> (/app/config/multer.js:7:17)
2020-10-17T05:24:18.106603+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1015:30)
2020-10-17T05:24:18.106604+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
2020-10-17T05:24:18.106604+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:879:32)
2020-10-17T05:24:18.106605+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:724:14)
2020-10-17T05:24:18.106605+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:903:19)
2020-10-17T05:24:18.106605+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:74:18)
2020-10-17T05:24:18.106606+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:9:16)
2020-10-17T05:24:18.106607+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1015:30)
...

Here is my code for multer:

require('dotenv').config();
const path = require('path');
const crypto = require('crypto');
const multer  = require('multer');
const GridFsStorage = require('multer-gridfs-storage');

const storage = new GridFsStorage({
  url: process.env.URI,
  options: { useUnifiedTopology: true },
  file: (req, file) => {
    return new Promise((resolve, reject) => {
      crypto.randomBytes(16, async (err, buf) => {
        if (err) return reject(err);

        const filename = buf.toString('hex') + path.extname(file.originalname);

        const fileInfo = { filename, bucketName: 'uploads' };
        
        resolve(fileInfo);
      });
    });
  }
});

const upload = multer({ storage });

module.exports = upload;

My process.env.URI variable is in the form: URI=mongodb+srv://...

Any help is greatly appreciated :)


Solution

  • I found the error was actually due to my environment variables. I had to set them up in Heroku for the application to read them (I was relying on my .env file, but of course the .gitignore file will prevent the file from being pushed to Heroku master).