Choosing a MongoDB field that has been received from a data context

My application creates a table by iterating through the events using #each events.

Each row in the table represents an event and contains 4 event tickets. When a user clicks on a ticket, a function is executed. For example, when the first ticket is clicked:

Template.displayEventsTable.events({

"click .buyTicket1": function () {

    buyEventTicket(1, this._id);

}

This is the buyTicket function:

function buyEventTicket (ticketNumber, eventID) {

    var buyer = Meteor.user();

    console.log(ticketNumber); // works
    console.log(eventID); // works
    console.log(eventID.eventTicketPrice); // currently returns "undefined"

  };
}

I am struggling to retrieve the eventTicketPrice, which is currently undefined. Despite going through the Meteor and HandleBars documentation, I have not been able to figure out the issue.

It seems illogical that the event ID is not returning the correct value for one of its fields.

What am I missing?

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

Answer №1

Utilize {{#with this}} prior to the {{> displayEventsTable}} and then close with {{/with}} in this manner:

<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>

Answer №2

Instead of passing only this._id to your buyEventTicket function, it would be better to pass the entire object. Simply pass this as an argument and you will be able to access all the object's attributes:

Template.displayEventsTable.events({
  "click .buyTicket1": function () {
    buyEventTicket(1,this);
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Executing the AngularJS nested controller only once

I am still new to Angularjs and just started working on an app that has several "projects" each with a local menu displayed on specific pages. The main navbar with footer is located in the Index.html: <body ng-app="ysi-app" ng-controller="MainControlle ...

jQuery alert: A TypeError was caught stating that the object being referred to does not have the 'dialog' method available

For a few days now, I have been tirelessly searching for a solution to this issue and it has caused me quite a bit of stress. My problem lies in echoing a JQuery popup script within php: echo '<link rel="stylesheet" href="http://code.jquery.c ...

Converting floating point numbers to fixed floating point numbers in JavaScript

Consider this scenario: I need to calculate 3 divided by 6, which equals 0.5 as a floating number. However, when I used the javascript toFixed(6) method to round it to 6 decimal points, it returned '0.500000' as a string instead of a floating num ...

Integrating TypeScript into an established create-react-app project

Struggling to integrate TypeScript into an existing create-react-app? I've always added it at the beginning of a project using create-react-app my-app --scripts-version=react-scripts-ts, but that's not working this time. The only "solution" I co ...

How can Vue JS 3 components exchange data between each other?

I am attempting to share data from a variable favorite_count in the Favorites component located in the file Favorites.vue. My goal is to pass this data to the App Component in the App.vue file but I have been unsuccessful so far. I want any changes made to ...

Creating a distinctive appearance for JavaScript's default dialogue box

Is there a way to enhance the design of my code that prompts the user for input using JavaScript's `prompt`? Currently, it appears too simplistic. Are there any CSS or alternative methods to improve its appearance? function textPrompt(){ var text = ...

Having trouble retrieving multiple selected values from the paper listbox in Polymer 3

I'm attempting to retrieve multiple selected values in a paper-listbox element in Polymer. <paper-dropdown-menu label="{{_getLabel('Activity Type')}}" id="fromMenu" on-paper-dropdown-close="fromAccountChanged" searchable="true"> ...

What causes the failure of making an ajax call tied to a class upon loading when dealing with multiple elements?

I can see the attachment in the console, but for some reason, the ajax call never gets triggered. This snippet of HTML code is what I'm using to implement the ajax call: <tr> <td>Sitename1</td> <td class="ajax-delsit ...

Manipulating information and connecting data on mongodb

Hello everyone, I am just getting started with Mongodb and have a question about two tables in my database. One table is called "records" and the other is called "files". In the "records" table, a row looks something like this: { "id" : ObjectId("6012d31ae ...

Using Vue.js to send user input data to a different page and trigger a method for submission

I am seeking assistance with my first question and hope to receive your support. In my setup, I have a catalogue page that includes a keyword search function and a main page with a search bar. My objective is to automatically redirect any submission from ...

Alter the class of the div element every three seconds

Greetings, I trust everyone is doing well. I am in need of some assistance from your brilliant minds. I have a circular div along with three CSS classes, and my objective is to switch the div's class and update the label text every 3 seconds. Any insi ...

What steps can be taken to ensure that the onchange event functions properly with radio input

I am facing an issue with my input field as the value changes automatically when an option is selected. However, I want this functionality to also work the same way when a radio input option is selected. How can I modify the code to achieve this? func ...

Imagine you are looking to execute an if/else statement

I'm having some trouble with this code that is meant to print different values based on certain conditions, but it only seems to be printing one value consistently. See the code snippet below: let twitterfollowers; let websitetraffic; let monthlyre ...

Retrieve error messages from API while utilizing Next-auth

Is it possible for Next-auth to handle API errors and pass them to the client-side? For example, our mobile app successfully handles API responses indicating incorrect email or password. However, on our website using Next-auth, we want to modify the retur ...

What are the best strategies for eliminating element cloning and duplication in Vue?

As a novice programmer, I've developed a web app similar to Trello. It allows users to create boards and within those boards, lists can be created. Each list is displayed uniquely with different IDs. However, the list items are displayed with the same ...

Encountering difficulties in loading my personal components within angular2 framework

I encountered an issue while trying to load the component located at 'app/shared/lookup/lookup.component.ts' from 'app/associate/abc.component.ts' Folder Structure https://i.stack.imgur.com/sZwvK.jpg Error Message https://i.stack.img ...

Is it normal for a Firebase listener to trigger twice when adding date to the database in React?

I've developed a basic chat application. Once the user submits a message, it is stored in the database along with their username. this.ref.child("/chat").update({username: this.state.username, message: this.state.chatMessage}); Subsequently, I ...

Creating dynamic Vue2 router links based on server-generated data

Currently, I am working on a Vue2 Single Page Application that fetches content from the server and allows clients to edit it in a Content Management System (CMS). The issue I'm facing is that when a user adds a relative link such as /about-us, Vue do ...

Refreshing the form fields and storing the information using AngularJS

I have successfully implemented a basic form using AngularJS. The issue I am facing is that even after the user enters their details and submits the form, the values remain in the input fields. My goal is to store the details of multiple fields in the con ...

Adding Bootstrap component via ajax request

I'm facing an issue with injecting a Bootstrap component using ajax. I usually include a select element like this: <select class="selectpicker" data-width="75%"> Most of the HTML code is generated dynamically through javascript, which you can ...