【问题标题】:Meteor - How to check if an image exists (server-side)Meteor - 如何检查图像是否存在(服务器端)
【发布时间】:2014-09-16 03:17:35
【问题描述】:

我的目标:如果不存在则显示默认图像。

我的方法:我创建了一个帮助器,它可以进行服务器端 Meteor.call 以检查图像 url 是否存在。帮助程序的目的是返回默认图像路径(不存在)或动态路径(图像存在)。

我被困在哪里

  1. Helper:在客户端,我可以成功 console.log 输出 从服务器端方法(result.statusCode)。但是,助手没有返回我的 模板中所需的字符串(/images/db/...等)。

  2. 方法:我得到 200 个结果 所有文件路径的状态,即使是那些不存在的。我猜测 这与 Iron-router 的全局 NotFound 模板有关,但不是 确定如何绕过它。我尝试使用 fs.exists 但永远无法 让它找到一个文件(所有响应都是错误的)。

非常感谢任何和所有建议。如果有更简单的方法可以做到这一点,我会全力以赴。


HTML:

<img src="{{imagePath key}}avatar.jpg">

我的帮手:

UI.registerHelper('imagePath', function(key){

  //Build the Meteor.call url
  var $host = document.location.host;
  var $imgBaseUrl = '/images/db/'
  var $assetPath = $imgBaseUrl + key + '/';
  var url = 'http://' + $host + $assetPath + 'bg.jpg';

  //Define the default image location
  var $assetPathDefault = $imgBaseUrl + 'default' + '/';

  //Call the server-side method
  Meteor.call('checkIfImageExists', url, function(error, result) {

    if (false) {
      console.log('Error');
      return $assetPathDefault;
    } else {
      console.log('Result: ' + result.statusCode);
      console.log($assetPath);
      return $assetPath;
    };
  });
});

服务器端方法

Future = Npm.require('fibers/future');

Meteor.methods({
  checkIfImageExists: function(url) {
    check(url, String);
    var fut = new Future();
    this.unblock();
    HTTP.get(url, function (error, result) {
      if (!error) {
        console.log('Found a file!: ' + url);
        console.log('Result: ' + result.statusCode);
        fut.return (result);
      } else {
        console.log(error);
        console.log('Error: ' + error);
        fut.return (false);
      };
    });
    return fut.wait();
  }
});

FWIW - 我正在将“url 检查”添加到一个旧助手中,该助手只是插入一个字符串而不检查图像是否存在。效果很好。

UI.registerHelper('imagePath', function(key){
  var baseUrl = '/images/db/';
  return baseUrl + key + '/';
}); 

【问题讨论】:

  • 使用 Future 使 HTTP.get 同步是在重新发明轮子,检查文档已经有一个同步版本:)
  • “我得到了所有文件路径的 200 个结果状态,即使是那些不存在的。” => 这可能是因为你的方法调用返回回调中的 if(false) 语句!
  • 感谢 cmets saimeunt...我会重新调用同步调用,看看是否可以让它工作。回复:200 个结果,如果我将 url 切换到我知道不存在的路径(例如在外部站点上),它确实提供了 404。如果您对 HTTP、fs.exists 和其他一些铁的解决方案有任何意见-路由器友好我仍然被困在那里。感谢您抽出宝贵时间。

标签: javascript meteor


【解决方案1】:

您的客户端助手不会返回任何内容!查看您使用的结构:

function a() {
  ...
  Meteor.call(..., function b() {
    return something;
  });
}

return something 是函数 b 的返回语句,而函数 a 没有返回语句 - 因此它返回未定义。

Meteor 服务器端方法是(并且必须是)异步的,而客户端 Javascript 的本质意味着辅助方法是同步的(浏览器中没有“等待”)。因此,要在客户端助手中使用服务器端方法,您必须利用反应性。幸运的是,使用ReactiveDict 很容易:

var imagePathDict = new ReactiveDict();

UI.registerHelper('imagePath', function(key) {
   ...
   if(!imagePathDict.get(key)) {
     // the path was not initialized, fetch it from the server
     Meteor.call(..., function(error, result) {
       ...
       imagePathDict.set(key, result.assetPath);
     });
   }
   // return the reactive path
   return imagePathDict.get(key);
});

 


顺便说一句,不要以$ 开头变量名(除非您引用一个jQuery 对象),这违反了Javascript 中的约定。

【讨论】:

  • 请注意:上次我检查 ReactiveDict 不是流星平台的一部分,因此请务必将 reactive-dict 添加到您的 .meteor/packages 文件中。
  • 这是非常有价值的休伯特,谢谢...你刚刚教了一个人钓鱼。
猜你喜欢
  • 2013-04-30
  • 2013-08-05
  • 1970-01-01
  • 2014-06-27
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
相关资源
最近更新 更多