CSCI 446 Unit #9
Preparation:
We will review some JavaScript then move on to Mongo
JavaScript
Download
Appendix D from the textbook. Some of this material is review, but it will
be helpful to read about JSON and also to fully understand callbacks
(used in many contexts, not just JS).
- Skim D.1 and D.2
- D.2.1 The Problem of Variable Hoisting. We've looked a hoisting, this
section shows a subtle variation. Could you explain it?
- D.2.1 Limiting Use of Global Scope. What the author is showing is
a common technique for adding a namespace to JavaScript
(remember a namespace give programmers a way to avoid
naming conflicts. For example, in C++, we write
using namespace std;
).
- More dynamic capabilities. Remember
that JS has dynamic variables (type can change during program
execution). This example also shows that new properties can be
added on the fly to JS Objects. This is very different from
C++/Java, where all variables are defined in a class
before any object can be created. What are some pros
and cons?
- D.2.2 Logic flow and loops through
D.2.3 Formatting Practices.
This is review material, just skim briefly. Example using
arrayLength
variable is something to consider.
- Read D.3 Getting to know JSON
- In listing D.2, why does "clean-shaven" need to be
in quote?
- What are the syntax rules for JSON? How do they
compare to JavaScript?
- Read D.4 Understanding JavaScript callbacks.
This is an important topic.
- What is an anonymous function? Why are they
useful?
- D.4.2 refers to an example from chapter 1.
The bank teller in this
analogy is like a node web server. Review chapter 1 if needed.
- Explain what it means for code to be asynchronous.
If this is a new concept to you, please type in the example code
from D.4.2, starting with
console.log("Taking Simon's request");
Experiment with different values for the delay. Asynchronous
is an important concept that's not too hard to understand, but
can be surprising when first encountered (and it's important to
recognize when commands are asynchronous... any time results
appear to be out of order, ask whether they might be handled
asynchronously).
- D.4.3. Notice that the first
parameter to
setTimeout
is a function. This is possible
because JS has first-class functions. We have already
seen that functions can be stored in a variable. Now we see that
functions can be passed as parameters. Functions can also be
returned from functions. Web programming makes extensive use
of these first-class function capabilities. NOTE: Function parameters in C++ allows
function pointers to be passed, but this approrach is not as flexible as
first-class functions.
- Callback scope. Be sure you understand
the example of passing
dollars
to the callback function (and
note that the anonymous function takes one parameter).
- D.4.4 Named callbacks and
Using variables from a different scope.
Challenge: the author has modified
setTimeout
in
this example. But setTimeout
is an existing function.
Modify this example so that getMoney
takes both
name and dollars and setTimeout uses an anonymous function that
takes no parameters.
- D.4.5 Callbacks in Node. Skim this, it
should help you understand some of the code in the textbook example.
Mongo
Install MongoDB if you haven't already.
Read Chapter 5 of Getting Mean. Some of the questions below
ask you to compare SQL and NoSQL approaches. These questions are not
critical for this course (we'll only use NoSQL), so anyone who has not
taken CSCI-403 can skip these questions. But for those who are familiar,
it's good to think about the differences.
- Figure 5.4 shows where Mongoose fits into the architecture.
- Notice that MongoDB creates the database when you try to use it.
This will be surprising to those familiar with most SQL databases.
- Text revision. In
db.js
, use:
mongoose.connect(dbURI, { useMongoClient: true })
to get
rid of deprecation warning related to open
vs openURI
.
- Be sure to read about how to close the mongoose connection. This is a lot
of typing, so for convenience the file is available here
(right-click and save). You may need to update the dbURI.
- A typical SQL database models data using rows and columns. How does
this differ from MongoDB?
- Be sure to understand Figure 5.7 and the NOSQL vocabulary: document,
collection, schema, path.
- What is the
_id
path?
- What schema types are supported? Please use camelCase for path names.
- Note that some validation can be specified in the schema
- We won't do much with google maps (and the info in the book is slightly
outdated), but maps are common in many websites, so it would be good for you to follow
along with the tutorial, just to have some exposure.
- Note how arrays are handled in NoSQL. How would this be done in SQL?
- Many applications need to handle dates effectively. Take a minute to read
and think about the "Options for storing time information" sidebar.
- Good software engineering: get familiar with tools that allow
you to view and update the database apart from your application. In this case,
we'll use the mongo shell AND the tool called Postman.
- EXPLORE!. The author shows how to add data using
db.locations.save
. I stored this
query, and a few others, in a file that I could copy and paste into the
shell. For convenience, here's my loc8r.json
file. Note that I added some coffee shops around where I live. It was fun
to use the https://whatsmylatlng.com
site to get the address for my home, and
http://mygeoposition.com to get coordinates for other addresses. You can
save the json file and either copy/paste into the mongo shell OR maybe edit
first with some of your favorite wifi locations. Then spend some time doing queries.
- NOTES related to MongoLab.
- I followed the sidebar so I would not need to supply a credit card.
If you do this, skip "Adding MongoLab to Heroku Application" and
"Getting the Database URI". You should still read/understand
"Breaking Down the URI into its Components".
- I had some issues when trying to set up the database user. Eventually
got it to work, I think there was some issue with the password I was
using. I also had to make my screen wider to see the Create New prompt,
and wasn't sure about some options (I chose Google and US Central, which
seemed to work)
- Follow the process carefully, be sure to write down info (e.g. user
id and password). I strongly suggest that you and your teammates
agree on a time you can meet to walk through this process together.
An extra set of eyes can often make a difference, and I may not be
able to help much remotely.
- MongoLab only allows one free database, so I suggest you give it a
generic name rather than calling it loc8r. For example, I named mine
car446-dev
. That way we can use the same account for
different lab assignments.
- Carefully walk through 5.5.2 Pushing up the data and 5.5.3 Making the
application use the right database. Be sure to test locally and on heroku.