如何修复 Multer-Gridfs-Storage 错误 "Error creating storage engine. At least one of url or db option must be provided"?
How do I fix Multer-Gridfs-Storage error "Error creating storage engine. At least one of url or db option must be provided"?
当我尝试将我的应用程序部署到 Heroku 时,我收到错误消息“创建存储引擎时出错。必须至少提供 url 或 db 选项之一。”在我的 heroku 日志中。但是,在我的 IDE.
中开发和测试应用程序时没有出现任何错误
这是我的错误日志:
...
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)
...
这是我的 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;
我的 process.env.URI
变量的形式是:URI=mongodb+srv://...
非常感谢任何帮助:)
我发现错误实际上是由于我的环境变量引起的。我必须在 Heroku 中设置它们以便应用程序读取它们(我依赖于我的 .env
文件,但是 .gitignore
文件当然会阻止文件被推送到 Heroku master)。
当我尝试将我的应用程序部署到 Heroku 时,我收到错误消息“创建存储引擎时出错。必须至少提供 url 或 db 选项之一。”在我的 heroku 日志中。但是,在我的 IDE.
中开发和测试应用程序时没有出现任何错误这是我的错误日志:
...
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)
...
这是我的 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;
我的 process.env.URI
变量的形式是:URI=mongodb+srv://...
非常感谢任何帮助:)
我发现错误实际上是由于我的环境变量引起的。我必须在 Heroku 中设置它们以便应用程序读取它们(我依赖于我的 .env
文件,但是 .gitignore
文件当然会阻止文件被推送到 Heroku master)。