【发布时间】: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