简体   繁体   English

从会话对象渲染模板

[英]Render template from session object

Im pretty new to Meteor, and programming in general, so if this is a ridiculous question I apologize. 对Meteor来说,我是一个陌生的人,而且对程序设计总体而言,因此,如果这是一个荒谬的问题,我深表歉意。

My program is pretty small at the moment so im just gonna post the whole thing because it's possible that what I think is the problem, may not be the real problem. 我的程序目前很小,因此我将发布整个内容,因为我认为可能是问题,可能不是真正的问题。

The app is for picking fights before an MMA event (who will win, what round etc).My issue is that you should be able to choose an event from the drop down (that event is set as a Session object), then my pickArea template should get the currently selected event from the session object and then render the options top pick from, for each fight. 该应用程序用于在MMA事件(谁将获胜,哪个回合等)之前进行打桩。我的问题是,您应该能够从下拉菜单中选择一个事件(该事件设置为Session对象),然后选择我的pickArea模板应该从会话对象中获取当前选定的事件,然后为每个战斗呈现最重要的选项。

I know the Session object is being set and updated correctly, but it seems like there's a problem connecting it to my pickArea helper, since I'm not getting my second console.log message. 我知道Session对象正在正确设置和更新,但是由于没有收到第二个console.log消息,似乎将它连接到我的pickArea帮助器时出现问题。 I think thats why none of my options are rendering. 我认为这就是为什么我的选项都没有呈现的原因。

I'm getting two errors from the console, but not sure how to use them to help me: 我从控制台收到两个错误,但不确定如何使用它们来帮助我:

Exception from Tracker recompute function:
TypeError: Cannot read property '0' of null

Here's my code: 这是我的代码:

Javascript Java脚本

//add Collections
  Results = new Mongo.Collection('results');
  Events = new Mongo.Collection('events');
  Picks = new Mongo.Collection('picks');

  if (Meteor.isClient) {

  Tracker.autorun(function () {
    console.log('The selectedEvent ID is: ' +
      Session.get('selectedEvents')
    );
  });  

  var user = "John"; //Just for testing purposes, will be replaced with actual user

  //get list of events to pick from (from Events Collection)
  //save selection in Session object
  Template.main.helpers({
    events: function () {
      return Events.find({}, {
        fields: {
          event: 1,
          _id: 1
        }
      });
    },
    isSelected: function () {
      return Session.equals('selectedEvents', this._id) ? 'selected' : '';
    }
  });
  //if they change the selection update the Session object
  Template.main.events = {
    'change #eventSelection': function (evt) {
      Session.set('selectedEvents', evt.currentTarget.value);
    }
  };

  //connect template to selected event  
  Template.pickArea.helpers({
    event: function () {
      return Events.findOne({
        _id: Session.get('selectedEvents')
      });
      console.log("This worked.. recieved " + Session.get('selectedEvents') )
    }
  });

  Template.pickArea.events({
    'click #save-button': function (event, template) {
      $('#fights').each(function() {

      var currentFight = fights.fightNumber; //might be wrong  
      var selectedEvent = Session.get('selectedEvents'); //dropdown selection (again not sure if done correctly)
      var fighterChoice = $('input[name="winner"]:checked').val()
      var finishChoice = $('input[name="finish"]:checked').val();
      var rdChoice = $('input[name="rd"]:checked').val();

      Picks.insert({
        user_ID:user, 
        event:selectedEvent, 
        fightNumber:currentFight, 
        fighter:fighterChoice, 
        finish:finishChoice, 
        round:rdChoice
        });

      });
    }
  });


  }

  if (Meteor.isServer) {
    Meteor.startup(function () {
      // code to run on server at startup

      //Empty the database and fill it with Test data
      Events.remove({});
      Picks.remove({});

      Events.insert({
        event: 'UFC 193',
        fights:[
          {
          fightNumber:1,
          fighter1: 'Stefan Struve',
          fighter2: 'Jared Rosholt',
          rounds: 3
        },
        {
          fightNumber:2,
          fighter1: 'Uriah Hall',
          fighter2: 'Robert Whittaker',
          rounds: 3
        },
        {
          fightNumber:3,
          fighter1: 'Mark Hunt',
          fighter2: 'Antonio Silva',
          rounds: 3
        },
        {
          fightNumber:4,
          fighter1: 'Joanna Jedrzejczyk',
          fighter2: 'Valerie Letourneau',
          rounds: 5
        },
        {
          fightNumber:5,
          fighter1: 'Ronda Rousey',
          fighter2: 'Holly Holm',
          rounds: 5
        }
        ]  
      });

      Events.insert({
        event: 'UFC 194',
        fights:[
          {
          fightNumber:1,
          fighter1: 'Max Holloway',
          fighter2: 'Jeremy Stephens',
          rounds: 3
        },
        {
          fightNumber:2,
          fighter1: 'Demian Maia',
          fighter2: 'Gunnar Nelson',
          rounds: 3
        },
        {
          fightNumber:3,
          fighter1: 'Ronaldo Souza',
          fighter2: 'Yoel Romero',
          rounds: 3
        },
        {
          fightNumber:4,
          fighter1: 'Chris Weidman',
          fighter2: 'Luke Rockhold',
          rounds: 5
        },
        {
          fightNumber:5,
          fighter1: 'Jose Aldo',
          fighter2: 'Conor Mcgregor',
          rounds: 5
        }
        ]  
      });

    });
  }

HTML 的HTML

<head>
    <title>fight_picks</title>
  </head>

  <body>
    <h1>Welcome to Meteor!</h1>
    {{> main}}
  </body>

  <template name="main">
      <p> Choose an event </p>
      <select name="event-select" id="eventSelection">
        {{#each events}}
        <option value="{{_id}}" {{isSelected}} >{{event}}</option>
        {{/each}}
      </select>  
    <div class="main-container">
      <h1>UFC Fight Picks Game! </h1>
      {{> pickArea}}  
    </div>
  </template>

  <template name="pickArea">
    <div class="each-fight">
      {{#with event}}
      <ul id='fights'>
      {{#each fights}}
        <li>{{> fightData}}</li>
      {{/each}}
      </ul>
      {{/with}}
      <br>
      <button id='save-button' > Save </button>  
    </div>
  </template>

  <template name='fightData' >
    <div class="slections">
      <p> Who do you think will win fight {{fightNumber}} ?
        <br>
      <input type="radio" name="winner" value="{{fighter1}}" checked/>
      {{fighter1}}
      <input type="radio" name="winner" value="{{fighter2}}" />
      {{fighter2}}
      </p>
        <br>
      <p> Finish Type?
        <br>
      <input type="radio" name="finish" value="DEC" /> 
      Decision
      <input type="radio" name="finish" value="KO/TKO" />
      KO/TKO
      <input type="radio" name="finish" value="SUB" />
      Submission
      <input type="radio" name="finish" value="Null" checked/>
      No Guess
      </p>
        <br>
      <p> Round?
        <br>
      {{#if fights.round 5}} <!-- if the # of rounds is 5 (may need gt?)-->
        <input type="radio" name="rd" value="1" />
        Round 1 
        <input type="radio" name="rd" value="2" />
        Round 2 
        <input type="radio" name="rd" value="3" />
        Round 3
        <input type="radio" name="rd" value="4" />
        Round 4 
        <input type="radio" name="rd" value="5" />
        Round 5
        <input type="radio" name="rd" value="Null" checked/>
        No Guess
      {{else}}
        <input type="radio" name="rd" value="1" />
        Round 1 
        <input type="radio" name="rd" value="2" />
        Round 2 
        <input type="radio" name="rd" value="3" />
        Round 3
        <input type="radio" name="rd" value="Null" checked/>
        No Guess
      {{/if}}
      </p>
    </div>  
  </template>

Thanks for any help, sorry about posting all this code but wanted to make sure that anyone wanting to help had enough info. 感谢您提供任何帮助,对于发布所有这些代码感到抱歉,但希望确保任何需要帮助的人都有足够的信息。 Also (not that anyone would want to) but if anyone wants to reuse any of the working parts of this code they are welcome to, if theyre making something similar. 同样(并非所有人都愿意),但是如果有人想重复使用此代码的任何工作部分,欢迎他们使用类似的东西。

This is embarrassing but I eventually realized that the problem was in my if block helper 这很尴尬,但我最终意识到问题出在我的if块帮助程序中

 {{#if fights.round 5}} <!-- This is not a real thing :( -->
    <input type="radio" name="rd" value="1" />
    Round 1 
    <input type="radio" name="rd" value="2" />
    Round 2 
    <input type="radio" name="rd" value="3" />
    Round 3
    <input type="radio" name="rd" value="4" />
    Round 4 
    <input type="radio" name="rd" value="5" />
    Round 5
    <input type="radio" name="rd" value="Null" checked/>
    No Guess
  {{else}}
    <input type="radio" name="rd" value="1" />
    Round 1 
    <input type="radio" name="rd" value="2" />
    Round 2 
    <input type="radio" name="rd" value="3" />
    Round 3
    <input type="radio" name="rd" value="Null" checked/>
    No Guess
  {{/if}}

apparently my syntax was wrong and I knew I wasn't 100% confident in it but i assumed if it was wrong it would still render the else part, but instead it just didn't render any of that template. 显然我的语法是错误的,并且我知道我不是100%对此有信心,但是我认为如果它是错误的,它仍然会呈现else部分,但是它只是不呈现任何该模板。 So I removed the if else tags and just used 5 rounds to see if it worked and it did. 因此,我删除了if else标签,并仅使用了5个回合来查看它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM