1
votes

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,
//...
1
you need to include what database you're currently using and perhaps show some of your current db code.Paul
I've updated with more info @Paulagaezcode

1 Answers

2
votes

Your 'images' property looks just like a single String value, so if you are looking for just a comma separated string of URLs the easiest thing would be to just bring in Lodash and create the string like so:

record.images = _.map(req.files['images'], 'path').join(',');

That said, I think in reality you don't want just a single string, but instead you'd do well (I think) to define an ImageSchema that includes some or all of the information you're getting from req.files. At minimum, I'd pull in the path and the mimeType, but other info could be useful to you, I don't know. In any case, given that you have an ImageSchema defined, you could do:

const Image = mongoose.model('Image');
/* your upload stuff happens, and then */

record.images = _.map(req.files['images'], function(i){ return new Image(i);});

That assumes of course that you update your RecordSchema to be:

...
images : [ImageSchema]
... 

Hope that helps.