Я могу загрузить файл на мою MongoDB, используя multer-gridfs-storage, но у меня возникают проблемы с привязкой файла к моей схеме "customer". По сути, у каждого клиента должно быть три типа файлов: сеть, электронная почта и файлы паролей.
Каждый файл будет в формате .txt. Моя конечная цель - создать страницу показа для каждого клиента и найти клиента по: id, а затем заполнить три файла клиента.
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const customerSchema = new Schema({
name: {
type: String
},
address: {
street: String,
city: String,
state: String,
zip: String,
},
fileID:
{
type: Schema.Types.ObjectId,
}
}, { timestamps: true });
module.exports = mongoose.model("Customer", customerSchema);
// Create mongo connection
const conn = mongoose.createConnection(process.env.MONGO_DB_URL);
// Init gfs
let gfs;
conn.once('open', () => {
// Init stream
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('customers');
});
// Create storage engine
const storage = new GridFsStorage({
url: process.env.MONGO_DB_URL,
file: (req, file) => {
return {
filename: file.originalname,
bucketName: 'customers'
};
}
});
const upload = multer({ storage });
post route to upload files
router.post('/upload', upload.single('file'), (req, res) => {
res.json({ file: req.file });
});