所以,基本上,我有这种方法,可以使用mulder上传图像,然后发送响应并为其创建副本。
想法是能够使用更新方法来更新我的数据库条目。
以下是架构:
const pictureSchema = new mongoose.Schema({
title: {type: String, maxlength: [50, 'Title must be longer than 50 characters']},
description: {type: String},
filename: {type: String},
thumbname:{type: String},
thumbswitch:{type: Boolean, default: false},
user: {type: mongoose.Schema.Types.ObjectId}
});
var picture = mongoose.model('Picture', pictureSchema);
exports.picture = picture;
这是实际方法:
pics.post('/upload', (req, res) => {
upload.single('image')(req, res, (err) => {
if(err){
return res.status(500).json({ message: err });
} else {
if(req.file == undefined){
return res.status(500).json({ message: 'upload a valid file!' });
} else {
let pic = new picture({
title: req.body.title,
description: req.body.description,
filename: req.body.databasepicname,
user: mongoose.Types.ObjectId(req.user._id)
});
pic.save()
.then(() => res.status(201).json({ message: pic }))
.then(() => gm(path.resolve('./storage/media/' + req.body.databasepicname)) //// TODO: establish correct routes,
.resize(120, null)
.write(path.resolve('./storage/media/thumb' + '-' + req.body.databasepicname), function (err) {
if (!err) {
console.log('thumbnail created successfully')
//THE PROBLEM IS HERE. HOW DO I UPDATE THE pic object?
pic.update($set{thumbname: 'thumb' + '-' + req.body.databasepicname,
thumbswitch: true})
pic.save().then(() => console.log(pic))
}
else{console.log(err)}
})
)
};
}
});
});
这是我尝试修改对象后通过记录图片获得的输出。
{ thumbswitch: false,
_id: 5ce85e820f08d96713c6dc73,
title: 'botty',
description: 'lorem ipsum blah',
filename: 'image-1558732418894.jpg',
user: 5ce85e820f08d96713c6dc72,
__v: 0 }
这是我希望得到的输出:
{ thumbswitch: true,
_id: 5ce85e820f08d96713c6dc73,
title: 'botty',
description: 'lorem ipsum blah',
filename: 'image-1558732418894.jpg',
thumbname: 'thumb-image-1558732418894.jpg',
user: 5ce85e820f08d96713c6dc72,
__v: 0 }
非常感谢所有帮助。非常感谢