2

My app generates a table using #each events.

There are 4 event tickets per row (each row represents an event) and when you click on, for example, the first ticket, this is what's executed:

Template.displayEventsTable.events({

"click .buyTicket1": function () {

    buyEventTicket(1, this._id);

}

This is the buyTicketfunction:

function buyEventTicket (whatNumber, whatEvent) {

    var buyer = Meteor.user();

    console.log(whatNumber); // works
    console.log(whatEvent); // works
    console.log(whatEvent.eventTicketPrice); // currently shows up as "undefined"

  };
}

The place I'm stuck with is trying to get the eventTicketPrice, which shows up as undefined. Despite reading through the Meteor and HandleBars docs, the light hasn't switched on yet as to what could be causing this.

It doesn't seem logical that the event ID wouldn't return the proper value for one of its own fields.

What have I missed?

EDIT 1

HTML Template

<template name="myEvents">
<h2>Events</h3>
          <table>
            <tr class="first-row">
              <th>Ticket 1</th>
              <th>Ticket 2</th>
              <th>Ticket 3</th>
              <th>Ticket 4</th>
            </tr>
            {{#each events}}
              {{#if eventsTypeA 1}}
                {{> displayEventsTable}}
             {{/if}}
            {{/each}}
          </table>
        </div>
      </div>
</template>

Events Table Template

<template name="displayEventsTable">
  <tr>
    <td><button class="buyTicket1 button-blue">Buy #1</button></td>
    <td><button class="buyTicket2 button-blue">Buy #2</button></td>
    <td><button class="buyTicket3 button-blue">Buy #3</button></td>
    <td><button class="buyTicket4 button-blue">Buy #4</button></td>
  </tr>
</template>

EDIT 2

_id: "YQXvUoBGKvhPObzFw"
createdAt: Sat Jan 02 2016 22:47:35 GMT+0100 (CET)
eventStatus: "open"
eventTicketPrice: 30
eventTickets: Array[4]
__proto__: Object
Optimae
  • 942
  • 1
  • 12
  • 23
  • @MichelFloyd I've added the HTML templates into the original post. `console.log(whatEvent)` returns the correct ID: I cross-checked it via the console by comparing it against the event ID in question, using `Events.find().fetch()`. (Events being the name of the MongoDB collection). – Optimae Mar 05 '16 at 22:59
  • I've added the object under EDIT 2. Do you mean the `myEvents` helper? – Optimae Mar 05 '16 at 23:10
  • 30 is what `console.log(whatEvent.eventTicketPrice);` needs to return, but it still currently returns `undefined`. When an event is created, it has the field `eventTicketPrice` with value 30 set - I've treble checked and there's no typo there. – Optimae Mar 06 '16 at 00:44

2 Answers2

1

you can use {{#with this}} before the {{> displayEventsTable}} then close with {{/with}} like this:

<template name="myEvents">
<h2>Events</h3>
          <table>
            <tr class="first-row">
              <th>Ticket 1</th>
              <th>Ticket 2</th>
              <th>Ticket 3</th>
              <th>Ticket 4</th>
            </tr>
            {{#each events}}
              {{#if eventsTypeA 1}}
                {{#with this}}
                {{> displayEventsTable}}
                {{/with}}
             {{/if}}
            {{/each}}
          </table>
        </div>
      </div>
</template>
0

You are only passing this._id to your buyEventTicket function, not the whole object. Just pass this in instead and then you can access the object's other attributes:

Template.displayEventsTable.events({
  "click .buyTicket1": function () {
    buyEventTicket(1,this);
});
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
  • This is the solution, thanks. I was under the erroneous impression that if you had just the ID of an object you could still access its fields anywhere, didn't realise you had to pass the whole thing. – Optimae Mar 06 '16 at 14:16