Click here to Skip to main content
15,891,033 members
Articles / Web Development / HTML
Tip/Trick

Hitching a Ride on the huMONGOus Meteor, Part 6 of 9

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
9 Aug 2015CPOL2 min read 6.3K   1  
Part 6 of the 9-part series "As the Meteor Blazes" - Reading MongoDB Data and Displaying it on the page

These Gangbangers Need a Wake-up Call

It's fine to enter <collectionName>.find().fetch() at the console to perform a quick-and-clean sanity check that your data is actually getting into your collection, but for more robust reading/querying/fetching/getting of data, we want to do it programmatically. Besides, in that way we can actually use the data in our app.

In the spirit of improvisational jazz, let's get right after it and dive right in. First, add this HTML in the .html file:

The HTM I've added is this:

HTML
  {{> placesLived}}

   . . .
   
<template name="placesLived">
  
  {{#each places}}
  
  {{/each}}<table style="width:60%"><tbody><tr><td> </td><td>{{ts_city}}</td><td> </td><td>{{ts_state}}</td><td> </td><td>{{ts_yearin}}</td><td> </td><td>{{ts_yearout}}</td><td> </td></tr></tbody></table>
</template>

...so that, for context, or for those who came late to the party, the entire .html file is now this:

HTML
<head>
  <title>timeandspace</title>
</head>

<body>

A List of the Places I Have Lived

{{> addTimeSpaceForm}} {{> placesLived}} </body> <template name="addTimeSpaceForm"> <template name="placesLived">

The Javascript I've added is this:

Template.placesLived.helpers({
  places: function () {
    // this helper returns a cursor of all of the documents in the collection
    return TimeAndSpace.find();
  }
});

...so that the entire .js file is now this:

TimeAndSpace = new Mongo.Collection('timeAndSpace');

if (Meteor.isClient) {
    Template.addTimeSpaceForm.events({
        'submit form': function(event){
            event.preventDefault();
            var city = event.target.city.value;
            var state = event.target.state.value;
            var yearin = event.target.yearin.value;
            var yearout = event.target.yearout.value;

            Meteor.call('insertLocationData', city, state, yearin, yearout);
        }
    });

	Template.placesLived.helpers({
		places: function () {
			// this helper returns a cursor of all of the documents in the collection
			return TimeAndSpace.find();
		}
	});
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
  
  Meteor.methods({
      'insertLocationData': function(city, state, yearin, yearout){
          console.log('attempting to insert a record');
          TimeAndSpace.insert({
              ts_city: city,
              ts_state: state,
              ts_yearin: yearin,
              ts_yearout: yearout
          });
      }
  });
}    

Get up, Meteor Man

Save (As) the .html and .js files, and watch the page refresh. Hopefully you will now have the documents "in hand." And here they are:

Image 1

In the so-called "real world," we would probably want to filter the results based on some criteria, but just for showing how to fetch documents, we will get any and all documents the collection contains. The code we can add to our .js file to do that is:

Also, this is still quite ugly or, to be magnanimous, plain, but we will work on that a little in a later episode. For now, we should be irrationally exuberant about being able to fetch and display the data.

In the next installment of "As the Meteor Blazes", we will gussy/spiffy up the page so it's not as much of an eyesore.

All Articles in the Series "Hitching a Ride on the HuMONGOus Meteor" (or, "As the Meteor Blazes")

PART 1: Installing Meteor, creating a Meteor project, and running the out-of-the-box Meteor Javascript App

PART 2: Making changes to the default HTML

PART 3: Creating a MongoDB Collection

PART 4: Creating the HTML to Receive Input from the User

PART 5: Writing MongoDB data

PART 6: Reading MongoDB Data and Displaying it on the page

PART 7: Gussying up/spiffifying the page with HTML and CSS

PART 8: Filtering and Ordering MongoDB Result Sets

PART 9: Meatier Meteor and MongoDB for Mutating Mavens

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
-- There are no messages in this forum --