Я пытаюсь сохранить изображение в отдельном каталоге и связать его с базой данных, но я совершенно потерян, потому что у меня есть подмассивы, в которых хранятся изображения.Вот мой код, чтобы объяснить это лучше.
request.js // Модель
whoCreated: String,
specialRequest: String,
contactInformation: {
name: String,
phone: String,
email: String,
requestDate: String,
},
installations: [{
ref: 'phase',
type: Schema.Types.ObjectId,
}],
эта установка имеет свой собственный шаблон и где изображения в основном хранятся ..
Installations.js // Модель
phase: {
text: String,
textKey: String,
index: String,
subjects: [{
id: String,
text: String,
textKey: String,
index: String,
comment: String,
image: { data: Buffer, contentType: String }, // which currently stores base64Image
imageName: String,
answer: String,
}]
// route.js
const multer = require('multer');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads/');
},
filename: function (req, file, cb) {
cb(null, new Date().toISOString() + file.originalname);
}
})
const upload = multer({ storage: storage })
router.post('/request/partial', upload.single('checklistImage'), async (req, res, next) => {
console.log(req.body.installations.subjects.base64Image);
const request= new Request({
whoCreated: req.body.whoCreated,
specialRequest: req.body.specialRequest,
contactInformation: req.body.contactInformation,
});
try {
const install = req.body.installations;
for (let i of install) {
const savedPhases = await InstallationRequest.insertMany(i);
for (let save of savedPhases) {
await partial.installations.push(save._id);
}
}
const saved = await request.save();
return res.status(201).json({
message: saved
});
} catch (e) {
return next(e);
}
});
Как я могу связать изображения на этом маршруте? Я не являюсьуверены, как связать req.file.path?