0

在我的网络应用程序中,一些“新闻和事件条目”有封面图像,有些没有(图像存储在 CollectionFS 中)。这是一个带有图像的示例(键是coverImageId):

JSON

{
    "_id" : "gDPwfDJFxh3y7NBBK",
    "title" : "EGOS 2015 reminder: Open Organizations for an Open Society?",
    "description" : "\n\t\t            \t\t\n\t\t            \t\tOpen Organizations for an Open Society? Practicing Openness in Innovation, Strategy and Beyond; Convenors: Leonhard Dobusch, Freie Universität Berlin, Georg von Krogh, ETH Zurich, Switzerland, Richard Whittington, Oxford University, Link to website: : http://bit.ly/EGOS15Open\n\t\t            \t\n\t\t            \t",
    "type" : "news",
    "createdAt" : ISODate("2016-02-28T20:30:09.316Z"),
    "coverImageId" : "teEfXMANGbSn4vWkD"
}

模板:

<div class="row text-left">
   {{#each newsEventsData}}
    <div class="col-sm-4">
       <div class="caption">
        {{#if imageExists}}     
          <h4>TRUE {{title}}<img alt="news thumbnail" class="news-thumbnail" src="{{ coverImage.url }}" /></h4>
        {{else}}
          <h4>FALSE {{title}}</h4>
        {{/if}}                                         
        <p class="text-muted">{{{description}}} <a href="{{pathFor route = 'newsAndEventsPage'}}" class="textlink">read more </a></p>
    </div>
   </div>
  {{/each}}
</div> <!-- ROW --> 

如果有图像,则显示图像。所以,这行得通。但是,该else部分永远不会被渲染。我假设我的助手渲染为 true,因为如果一个文档包含一个coverImageId.

帮手:

Template.allNewsEvents.helpers({
  newsEventsData: function () {
    if(Session.equals("newsEventsSeeMore","no")) {
       return NewsEvents.find({type: { $in: Session.get('newsEventsViewMain')}}, {sort: {createdAt: -1}, limit: 3});
    }
    else {
      return NewsEvents.find({type: { $in: Session.get('newsEventsViewMain')}}, {sort: {createdAt: -1}});
    }   
  },
  imageExists: function () {
    return NewsEvents.find({_id:this._id},{coverImageId: { $exists: true } });
  }
}); 

任何帮助都非常感谢。

4

1 回答 1

1

find返回一个游标,它是一个对象,并且总是计算为真。你要:

  imageExists: function () {
    var event = NewsEvents.findOne( {_id:this._id} );
    if( event && event.coverImageId ) {
      return true;
    } else {
      return false;
    }
  }
于 2016-03-02T20:16:18.940 回答