【问题标题】:Update image using collectionfs使用 collectionfs 更新图像
【发布时间】:2023-03-29 10:37:01
【问题描述】:

我正在使用 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 更改为 Images.update,我已经阅读了流星文档但找不到如何操作。任何人都可以提出一种方法或一些文档来让我学习如何做吗?

提前致谢!

【问题讨论】:

  • 说到不明白怎么update,你不是刚写了更新声明Meteor.users.update(currentUserId, {$set: imagesURL});吗?你能更具体地说明你想做什么吗?如果Imagess 是一个mongo 集合,您可以执行与Meteor.users 相同的操作。
  • 我对更新语句有了基本的了解。我想要做的是更新图像,是 Imagess.update(currentUserId, {$set: newFile, function(err,fileObj)?

标签: node.js mongodb meteor


【解决方案1】:

那会是 Images.update(currentUserId, {$set: newFile, function(err,fileObj)?

没有。


Meteor 的 Mongo 集合更新语句的语法仿照that of mongo Shell's。在这种情况下,如果要进行更新,则需要在第一个参数中指定要更新的记录。

Imagess.update({_id:theImageId},newFile);

在 mongo 中,更新实际上只是覆盖。

这会将满足语句 _id:theImageId 的图像中的记录覆盖到新的 JSON 对象 newFile 中。但你确定那是你想要的吗?如果您希望能够更改用户的图像,此语句已经为您完成了:

Meteor.users.update(currentUserId, {$set: imagesURL});

如果这确实是您想要的,那么您必须以某种方式获取您想要首先更新的 Imagess 数据记录(或它的任何属性)的 id。

注意:insert 语句返回插入数据记录的id。

【讨论】:

    【解决方案2】:

    没有办法使用 FSCollection(在本例中为图像)更新当前 url 图像,请查看 this Github Issue,其中 Raix 和 Aldeed 谈到了一些未来的工作,例如 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")
            }                
       }
    })
    

    【讨论】:

      猜你喜欢
      • 2018-12-31
      • 2015-06-02
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多