对于刚上传的图像,第一个请求返回503到Meteor GridFS

时间:2015-05-07 19:16:56

标签: javascript meteor gridfs

有谁能告诉我为什么会发生这种错误以及如何解决? 当我重新加载页面(cmd + r)时,图像请求工作正常。

来自浏览器控制台的错误:

http://xxxxx.meteor.com/cfs/files/images2/aEaqKMjLQZcDPHLS8 503(服务不可用)

会发生什么:

  1. 图片已上传(gridFS)
  2. url已添加到用户个人资料
  3. img src =“{{imgsrc}}”被重新更新,因此是一个http请求。
  4. 请求网址正确但结果为503
  5. 重新加载页面,错误消失。
  6. 上传图片的代码

    Template.profileedit.events({
    'change .myFileInput': function(event, template) {
    var files = event.target.files;
    
    for (var i = 0, ln = files.length; i < ln; i++) {
      Images.insert(files[i], function (err, fileObj) {
    
    
          var userId = Meteor.userId();
          var imagesURL = {
            "profile.image": "/cfs/files/images2/" + fileObj._id
    
          };
          Meteor.users.update(userId, {$set: imagesURL});
    
    
      });
    
    
    }
    
    
    }
    

    模板助手

    Template.profileedit.helpers({
      imgsrc: function() {
    
        var user = Meteor.users.findOne({"_id" : Meteor.userId()});
    
        return Meteor.user().profile.image;
    
    
      }
    });
    

    显示图片的HTML:

    <template name="profileedit">
      <img src="{{imgsrc}}" style="width:400px" >
    </template>
    

    其他内容

    本地和meteor.com上的问题都是一样的 不安全和自动发布是活跃的(这只是学习步骤)

    由于 的Jesper

1 个答案:

答案 0 :(得分:2)

我想你会遇到Mongo尚未准备好检索对象的问题。我的猜测是,在您的图像实际可用于检索之前发生了被动更新,但是当您点击刷新时它就在那里并准备好被检索,这就是为什么可以解决这个问题。

有几种方法可以解决这个问题。例如:https://github.com/CollectionFS/Meteor-CollectionFS基本上允许您设置“等待”图像,该图像将在您的文件未就绪时显示,并在您的图像设置为检索时进行反应性更新。您也可以自己执行此操作,并显示您的实现。

相关问题