I am uploading some files to my MongoDB database but currently my upload.js middlware fileInfo object only accepts a single static bucket string since I cannot dynamically change the bucketName string. How can I pass the upload.js middeware any string I want for the bucketName?
ACTION:
export const uploadPhoto = (img) => (dispatch) => {
const formData = new FormData();
formData.append("file", img);
const bucketName = "photos"; //HOW DO USE THIS BUCKET NAME IN THE upload.js Middlware?
axios
.post("/video/upload", formData)
.then((res) => {})
.catch((err) => {
console.log(err);
});
};
ROUTES:
const router = require("express").Router();
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var ObjectId = require("mongodb").ObjectId;
const { authUser } = require("../middleware/auth");
const uploadMulter = require("../middleware/upload");
//HOW DO I PASS DATA TO THE uploadMulter Middleware?
router.post("/upload", authUser, uploadMulter, (req, res) => {
console.log(req.file);
});
Upload Middleware:
const path = require("path");
var mongoose = require("mongoose");
const crypto = require("crypto");
const multer = require("multer");
const { GridFsStorage } = require("multer-gridfs-storage");
const db = require("../config/.env").mongoURI;
const storage = new GridFsStorage({
url: db,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
const filename = buf.toString("hex") + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: "HOW DO I CHANGE THE BUCKETNAME?", //<--?
metadata: {
author: req.user._id,
},
};
resolve(fileInfo);
});
});
},
});
const upload = multer({ storage });
module.exports = upload.single("file");