When done, submit at here.

1


2


3


4


5


6


7


8


9


10


11


12


13


14


15

Stretch Goals

Add comments to your projects.

Do this by adding a new schema and linking it to the project schema


16

How will this lab help me?

This lab will get you well-versed in using databases. They will form an important portion of your app and future assignments.


17


18


19


20


21


22


23


24


25


26

Video


27


28

Code

newPost.save(afterSaving);

function afterSaving(err) { // this is a callback
  if(err) {console.log(err); res.send(500); }
  res.redirect('/');
}

29


30


31


32


33


34

Video


35


36


37

Mongoose cheatsheet


38


39

Debug

When I run node app.js I get the following error

Error: failed to connect to [localhost:xxxxx]

Mongo isn't running! Did you remember to run the check-setup.sh script?

Debug

When I go to localhost:3000, there are no placeholders like described in the lab slides?

Once you define the schema (next 4 slides), they appear!


40


41


42


43

Debug Hints

I restarted the server, but nothing shows!

In index.js, add a console.log(projects) inside renderProjects. Is your data formatted correctly? Perhaps you need to change the schema. Don't forget to rerun node initDB.js after changing your schema.


44


45


46

Debug

I can't access res in my callback.

You need to declare the callback function within the projectInfo function to get access to res.

Remember that the ID field is automatically generated and is called "_id"


47

Video


48


49


50

Video

Debug Hints

Doesn't work and I can't findout why!

In your callback, make sure there's a 'if (err) { console.log(err) }'. What does the console output say?

When I'm working on the delete part , everything is deleted when I click on the delete button. Is there a way to get them back?

You can run node initDB.js. It will reset the db for you.

Callback Structure

function deleteCallback(err) {
  if(err) { console.log(err); }
  ...
}

51


52

Debug Hints

When I add a project using the form, the new project gets added to the top instead of the bottom of the page as it's supposed to. How can I fix this?

The mongoose date constructor seems to choose a random date when only given a month and day. The query displaying the results is sorting by date, if you add a year to your date input it will use that to create the date and place the image in the correct location.

The dates for all the projects are displayed as a day before its actual date.

It is just due to time zone stuff (we are in pacific time not UTC) so the days are off by like 8 hours and that messes with the dates.


53

Video

Debug Hints

Doesn't work sand I can't findout why!

In your callback, make sure there's a 'if (err) { console.log(err) }'. What does the console output say?

Callback Structure

function addCallback(err) {
  if(err) { console.log(err); }
  ...
}

54


55

GitHub - Commit

git status
git add ...
git commit -m "putting it together lab"

Github - Push

git push

56

Video

Debug

I don't really want to give my credit card to a company I dont trust and am not buying a product from. Is there any alternative to adding this addon?

We don't know of any other way!


57

Heroku - Create

heroku create newapplicationname

Heroku - Addon for Mongo

heroku addons:add mongolab

Heroku - Push

git push heroku master

Heroku - Initialize the Database

heroku run node initDB.js

58


When done, submit at here.