使用node.js引用多个文件上载到DB

使用node.js引用多个文件上载到DB,node.js,jquery-file-upload,multer,Node.js,Jquery File Upload,Multer,我不明白如何使用Node.Js保存上传到数据库的多个文件的引用(文件名) 我正在使用blueimp jquery文件上传和multer中间件将多个文件上传到本地存储(uploads文件夹) recordController.js代码: exports.postUpdate = ((req, res, next) => { Record.findById(req.params.id, (err, record) => { if (req.files ) {

我不明白如何使用Node.Js保存上传到数据库的多个文件的引用(文件名)

我正在使用blueimp jquery文件上传和multer中间件将多个文件上传到本地存储(uploads文件夹)

recordController.js代码:

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输出:

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 } ]
更新 我使用MongoDB作为数据库,mongoose作为ODM

更多信息:目前,我正在使用jquery文件上传,当我上传2张图片时,它会发出2个post请求。如果没有jquery文件上传,它会对上传的所有图像生成一个post-req,并且我会得到一个唯一的req.files数组(所有文件都上传了)。如何将该信息保存到MongoDB

recordModel.js

//...
  images: String,
//...

您的“images”属性看起来就像一个字符串值,因此,如果您只查找一个逗号分隔的URL字符串,最简单的方法是引入Lodash并创建如下字符串:

record.images = _.map(req.files['images'], 'path').join(',');
也就是说,我认为实际上您不希望只需要一个字符串,相反,您最好(我认为)定义一个ImageSchema,其中包含从
req.files
获得的部分或全部信息。至少,我会输入路径和mimeType,但其他信息可能对你有用,我不知道。在任何情况下,如果已定义ImageSchema,则可以执行以下操作:

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

record.images = _.map(req.files['images'], function(i){ return new Image(i);});
这当然假设您将RecordSchema更新为:

...
images : [ImageSchema]
... 

希望有帮助

您需要包括您当前使用的数据库,并可能显示您当前的一些db代码。我已经更新了更多信息@Paul
const Image = mongoose.model('Image');
/* your upload stuff happens, and then */

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