【问题标题】:Instead of saving inside array, data is getting stored inside new record [duplicate]数据不是保存在数组中,而是存储在新记录中[重复]
【发布时间】:2018-02-11 18:05:54
【问题描述】:


我正在使用 MEAN 堆栈和 Multer 模块上传图片。
我能够从 Angular 中检索图像,甚至可以将图像路径发布到 Mongoose 集合。

问题是我期待一组图像,但是在发布到猫鼬时,它会将每个图像存储为新记录。

图像架构

var imageSchema=new Schema({
    productId:{type: String,required: false},
    imagePaths: [{type: String, required: false}]
});



POST API

router.post('/upload', upload.any(), function(req , res){
  console.log('Executing Upload API..');
    console.log(req.body);
    console.log(req.files);
    var images = req.files;

req.files.forEach(function(file){
      var filename = (new Date()).valueOf() + '-' + file.originalname;
      fs.rename(file.path,'public/images/'+ filename, function(err){
       // if (err) throw err;
        //Save to mongoose

        var image = new Image({
          productId: 1007,
          imagePaths: filename
        });
        image.save(function(err, result){
          if(err) throw err;
            res.json(result);
        });
        console.log('FileName :' + filename);

      });
    });
});



收藏已保存
如果我发布 2 张图片,它会按如下所示进行存储,但我希望这两张图片都存储在同一个记录中,即在imagePaths:.

**

{
        "_id" : ObjectId("59abab004783d90bccb4a723"),
        "productId" : "1007",
        "imagePaths" : [
                "1504422656691-Screenshot (4).png"
        ],
        "__v" : 0
}
{
        "_id" : ObjectId("59abab004783d90bccb4a724"),
        "productId" : "1007",
        "imagePaths" : [
                "1504422656691-Screenshot (3).png"
        ],
        "__v" : 0
}

**
请帮忙。

【问题讨论】:

    标签: angularjs node.js mongodb mongoose


    【解决方案1】:

    在您的forEach 中,您正在为带有new Image 的每个文件创建新记录,而您应该做的是创建一个包含所有文件名的数组并创建一次记录。也许这段代码会对你有所帮助。

    router.post('/upload', upload.any(), function(req , res){
      console.log('Executing Upload API..');
      console.log(req.body);
      console.log(req.files);
      var images = req.files;
    
      const filePromises = req.files.map(function(file){
        var filename = (new Date()).valueOf() + '-' + file.originalname;
        console.log('FileName :' + filename);
    
        return new Promise(function(resolve, reject) {
    
          fs.rename(file.path,'public/images/'+ filename, function(err) {
            if (err) return reject(err);
            return resolve(filename);
          });
        });
    
      });
    
      Promise.all(filePromises)
      .then( fileNames => {
    
        var image = new Image({
          productId: 1007,
          imagePaths: fileNames
        });
    
        image.save(function(err, result){
          if(err) throw err;
          res.json(result);
        });
      })
    
    });
    

    在此我创建了包含文件名的 promise 数组,然后使用 Promise.all 解析所有这些,最终得到解析后的文件名数组,然后我可以简单地传递它来创建新记录。

    【讨论】:

    • 谢谢,它按预期工作。
    • 如何使用angular js显示上传的图片?
    • 我对 Angular 不太熟悉。但是由于你是在public文件夹中保存图片,所以可以直接使用img src中的public文件夹路径。
    • 好的,谢谢,我会试试的。
    猜你喜欢
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 2017-11-23
    • 2015-05-26
    • 2018-09-26
    • 2015-07-05
    • 1970-01-01
    • 2012-01-02
    相关资源
    最近更新 更多