Search code examples
meteorcollectionfs

CollectionFS 503 Service Unavailable


I need to upload 'on fly' user's image. But i get error 503 Service Unavailable.

user.js

Meteor.subscribe('userImages');

Template.userProfil.events({
  'change [name=userPhotoUpload]': function(event) {
    event.preventDefault();
    FS.Utility.eachFile(event, function(file) {
        var newFile = new FS.File(file);
        newFile.metadata = {
            createdBy:Meteor.userId(),
        }
        userImages.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.userImg": '/cfs/files/userImages/' + fileObj._id + '/' + fileObj.name()
                };
                Meteor.users.update(currentUserId, {$set: imagesURL});//there I get url and

            }
        });
    });

  }
});

router.js

Router.route('/organizer', {
  name: 'userProfil',
  template: 'userProfil',
  data: function() {
      var currentUser = Meteor.userId();
      return Meteor.user({_id: currentUser});
  }
});

user-img.html

<img src="{{profile.userImg}}">

after uploding image i get this err:

http://localhost:3000/cfs/files/userImages/wNjvF8uuN8j6fd8md/exampl2.jpg 503 (Service Unavailable)

But this path is absolutely correct, and after manual reloading page it's work.

How can I solve this problem?


Solution

  • Ok, I found some solution, but I don't think that it's correct way. Maybe someone have better decision?

    Changing:

    user.js

    'change [name=userPhotoUpload]': function(event) {
        event.preventDefault();
        FS.Utility.eachFile(event, function(file) {
            var newFile = new FS.File(file);
            newFile.metadata = {
                createdBy:Meteor.userId(),
            }
            userImages.insert(newFile, function (err, fileObj) {
                if (err){
                 // handle error
                } else {
    
                    var currentUserId = Meteor.userId();
                    var intervalHandle = Meteor.setInterval(function () {
                                console.log("Inside interval");
    
                                // changes here:
    
                                if (fileObj.hasStored("userImages")) {
                                     //checked if image was stored
                                    var imagesURL = {
                                      "profile.userImg": '/cfs/files/userImages/' + fileObj._id + '/' + fileObj.name()
                                    };
                                    Meteor.users.update(currentUserId, {$set: imagesURL});
                                    // if file has stored, stop interval
                                    Meteor.clearInterval(intervalHandle);
                                }
                            }, 1000);
                }
            });
        });
    
    
    }