我正在使用 collectionFS 上传图片,问题是我希望我的用户能够只上传一张图片然后更改它(上传一张新图片)。 我有检查图像是否已经上传的部分,但问题是我无法更改数据库中的图像,这是我必须插入新图像的代码:
'change .myFileInput': function(event, template) {
FS.Utility.eachFile(event, function(file) {
var newFile = new FS.File(file);
newFile.metadata = {
createdBy:Meteor.userId(),
}
Imagess.insert(newFile, function (err, fileObj) {
if (err){
// handle error
} else {
// handle success depending what you need to do
var currentUserId = Meteor.userId();
var imagesURL = {
"profile.image": "/cfs/files/images/" + fileObj._id
};
Meteor.users.update(currentUserId, {$set: imagesURL});
}
});
});
我不知道如何将 Imagess.insert 更改为 Imagess.update,我已经阅读了 meteor 文档,但找不到如何操作。任何人都可以建议一种方法或一些我可以学习如何做的文档吗?
提前致谢!
最佳答案
无法使用 FSCollection 更新当前 url 图像(在本例中为图像),检查 this Github Issue ,其中 Raix 和 Aldeed 讨论了一些 future 的工作,例如 FS.File.updateData()
,但尚未实现。
可能的解决方法是这样。
Template.example.events({
'click #changeImage':function(event,template){
var message = confirm("Do you wanna change this image?");
if(message == true){
var file = $('#changeImageInput').get(0).files[0],
newFile = new FS.File(file);
newFile.metadata = {
createdBy:Meteor.userId(),
}
var query = Images.findOne({'metadata.createdBy':Meteor.userId()}) //supposing there is only one image if not use a .fetch() and a for instead.
//removing the current image.
Images.remove({_id:query._id},function(err,result){
//if there is not error removing the image, insert new one with the same metadata
if(!err){
Images.insert(fsFile,function(){
if(!err){
console.log("New image get upload")
}
})
}
});
}else{
console.log("user don't want to change the image")
}
}
})
关于node.js - 使用 collectionfs 更新图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29479259/