Click here to Skip to main content
15,885,537 members
Articles / Programming Languages / Javascript
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
9 Aug 2015CPOL2 min read 6K  
Part 5 of the 9-part series "As the Meteor Blazes" - Writing MongoDB data

How Can We Stop the Crime and the Gangs if We Act Like We Don't See Them?

While there are other, geekier, ways to insert data, the one that seems most practical to me, and thus the one I will show you, is to do so via user input on the web page (you can also insert Documents (remember, that's a "row") into the Collection (and that that's a "Table") from the Dev Tools console, from the command line, or via the .js file).

This is how the .js file looks after I've added the code to insert the values on the form whose HTML was shown in part 4 of this series:

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);
        }
    });
}

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

As you can see, the client block responds to the submit button submitting the form, which calls a method that is in the server block. This handles the insertion of the document.

But how do you know that your record actually got inserted, as you so fervently hope? In the Dev Tools (YBMV, but I am using Chrome, and thus access Chrome Dev Tools via F12), enter:

TimeAndSpace.find().fetch()

If/when this doesn't show you all the documents you have inserted into the collection, add an int arg to the fetch(), such as:

TimeAndSpace.find().fetch(42)

That will return an array of Objects; click on them to expand, and you will end up with something like the following:

Image 1

No One Must Ever Know the True Identity of Meteor Man!

Due to the peripatetic ways of my parents and myself, not all of the places I have lived display on the scream shot above, but all eight states do, at any rate. Anybody out there live in the same place at the same time as me?

in the next installment of "As the Meteor Blazes", we will read the MongoDB data (using Javascript, rather than from the console using <CollectionName>.find().fetch() as shown above).

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