MongoDB
 sql >> Base de Dados >  >> NoSQL >> MongoDB

Atualizar imagem usando collectionfs


Não há como atualizar uma imagem de URL atual usando FSCollection (neste caso, a imagem), verifique este problema do Github , onde Raix e Aldeed falam sobre alguns trabalhos futuros como FS.File.updateData() , mas ainda não foi implementado.

Uma solução possível será esta.
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")
        }                
   }
})