我可以使用 multer-gridfs-storage 将文件上传到我的 MongoDB,但是我无法将文件与用户关联起来,我想要的是能够将从 passportjs 获得的 req.user 传递给文件元数据
// Create mongo connection
const conn = mongoose.createConnection(mongoURI);
// Init gfs
let gfs;
conn.once('open', () => {
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('uploads');
});
// Create storage engine
const storage = new GridFsStorage({
url: mongoURI,
file: (req, file) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
const filename = buf.toString('hex') +
path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'uploads',
metadata: {
user : // I want to pass here req.user
}
};
resolve(fileInfo);
});
});
},
});
const upload = multer({ storage });