【问题标题】:why schema fields is not saving in database using node js为什么模式字段没有使用节点 js 保存在数据库中
【发布时间】:2021-07-17 11:34:18
【问题描述】:

我正在尝试将多个字段文件保存到 mongodb 中,目标路径正在保存,但字段没有保存在 mongodb 中,我做错了什么,请帮助我提前谢谢

架构:-

{   
 thumbnail: { type: String, data:Buffer},
 trailerVideo : { type: String, data:Buffer}, 
}

创建一个实例保存在 mongodb 中:-

const courseFields = new Courses({  
    thumbnail: req.files.path,
    trailerVideo: req.files.path
});
courseFields
  .save(courseFields)

混合数据:-

     const multer = require("multer"),
     storage = multer.diskStorage({
        destination: function(req, file, cb) {
          cb(null, 'uploads')
        },

       filename: function (req, file, cb) {
         cb(null, file.originalname);
       }
    });


 const uploadImg = multer({
     storage: storage
  }).fields([{name:'thumbnail'},{name:'trailer'}]);

邮递员:-

【问题讨论】:

    标签: node.js mongodb multer


    【解决方案1】:

    req.files 是一个array,所以你应该指定一个文件索引来获取文件路径。 如果每个字段中只有一个文件。你的实例应该是:

    const courseFields = new Courses({  
        thumbnail: req.files[0].path,
        trailerVideo: req.files[0].path
    });
    courseFields
      .save(courseFields)
    

    或者每个字段有多个文件。您可以遍历它以获取路径列表并将它们存储在数组中。

    参考: multer docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 2016-10-21
      • 2021-11-12
      • 2018-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多