I can't understand how to save references (filenames) of multiple files upload to the database using Node.Js.
I'm using blueimp jquery file upload and multer middleware to perform a multiple files upload to the local storage (uploads folder).
<input id="fileupload" type="file" name="images" data-url="/record/edit/#{record.id}" multiple>
app.js code:
//using multer to upload files
const upload_images = upload.fields([{ name: 'featured_img', maxCount: 1 }, { name: 'images', maxCount: 8 }]);
//...
app.post('/record/edit/:id', upload_images, recordController.postUpdate );
recordController.js code:
exports.postUpdate = ((req, res, next) => {
Record.findById(req.params.id, (err, record) => {
if (req.files ) {
console.log('UPLOADED')
// record.featured_img = req.files['featured_img'][0].filename; //=> working for single file
// Now, how to make it work for multiple files?
console.log(req.files['images'])
record.images = "HOW TO SAVE THE INFO OF REQ.FILES TO DB?";
}
console.log output:
UPLOADED
[ { fieldname: 'images',
originalname: 'slippry-02.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'C:\\Users\\agaez\\Documents\\sanbox\\nodeApps\\theapp\\uploads',
filename: '8mdnuacm1469201049450.jpg',
path: 'C:\\Users\\agaez\\Documents\\sanbox\\nodeApps\\theapp\\uploads\\8mdnuacm1469201049450.jpg',
size: 49798 } ]
POST /record/edit/578580b43c7a08601730cc06 302 26.654 ms - 82
UPLOADED
[ { fieldname: 'images',
originalname: 'slippry-03.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'C:\\Users\\agaez\\Documents\\sanbox\\nodeApps\\theapp\\uploads',
filename: 'yrnnzl9h1469201049467.jpg',
path: 'C:\\Users\\agaez\\Documents\\sanbox\\nodeApps\\theapp\\uploads\\yrnnzl9h1469201049467.jpg',
size: 49792 } ]
Update
I'm using MongoDB as database and mongoose as ODM
More info: Curretly, I'm using jquery file upload which is making 2 post request when I upload 2 images. Without jquery file uploads it makes 1 post req for all the images uploaded and I get the req.files a unique array (with all the files uploaded). How can I save that info to MongoDB?
recordModel.js
//...
images: String,
//...