OK so back to the issue of connecting to our database.
The problem again is that locally remember our databases.
Mongo is this server right here that we're connecting to using mongoose.
This is local and Heroku our app on her who has no access to this at all.
So the solution that we're going to go with is to use a hosted Mongo database using something called
Mongo lab Samaniego lab is basically just an instance of Mongo running on a server somewhere that we
can get a URL from that rather than being a local host.
It will be mango Lahab something something something you are all that we can use in our application
that exists outside of this local environment that actually exists online somewhere.
So Mongo lab dot com before you get started this is not the only solution.
This is just a really simple common solution.
You can get Mongo installed locally on the Heroku instance.
It's kind of a pain and it has its own set of problems.
So this is a really pretty standard tool to use and the other thing is that it gives you great visual
interface too to interact with your database to manage permissions to do things like reset your database
.
Heaven forbid if you ever needed to do that or look at different pieces of information and spects things
.
It gives you a lot of cool tools.
So sign up.
You also need to use a real real email address so I'll just call this web dev boot camp
username and just be cold email
and then add your password quick except you have to read this apparently.
Open this at least then click accept and then click click Create account.
So we're now in Monga lab.
The next thing that we need to do is verify our e-mail address.
So go ahead and do that.
Open up your email account.
Click the link that they send you.
OK.
So I went ahead and verified my e-mail.
You can see here my e-mail address has been verified.
Now I have access to these buttons.
So this is our home page or dashboard that allows us to create new Mongo deployments which is the first
thing that we want to do.
So basically we're just making another database that lives on Mongo lab servers rather than our own
to go ahead and click on Create new take a little bit sometimes.
Great.
So this lets us do now is create our new database on Mongo Lab. what we're going to do is stick with
Amazon Web services but rather than replica set cluster that is a powerful high speed high availability
yet expensive as you can see they get really expensive options we're not going to do that.
We'll stick with single node.
And for now we'll just do sandbox.
So that's the free plan then what we're going to do is create a name for our database.
We'll just call it the up came up and needs to be lowercase camp $0 month.
Looks good to me.
On create wait for this to finish up.
All right.
So if started.
We're almost there as far as what we need to do on Monga lab and we can click on this the 0 5 5 whatever
the you are all that you get there and you'll see that it gives us this new URL.
So if I just copy this URL it's not going to work just yet.
But I just want to show it to you.
So if I copy it and go back to your camp Where's that.
Here we go.
And just pasted below.
Notice that it's very similar to this.
It's a mongo D-B Yoro.
However instead of localhost it's Monga lab dot com slash Yup camp.
And there's also this D-B user D-B password that we're going to have to take care of.
So that's why I said we're not quite done.
So I'm going to go back and we need to start and actually Telsey this database user is required to connect
to this database.
Click here to create a new one.
So we need to have a user.
Basically the way that this works is set on Lango lab.
You can have different users different permissions.
We're not talking about users of our application.
Basically developers.
So people who can connect to this database you might want to have two apps that use the same database
.
You know you have like sales data that you've captured a bunch of leads and you want to have marketing
but you also want your e-mails to.
I don't know if you want to have different people.
Different teams have access to the same database maybe with different permissions you can actually manage
that here.
But we're just going I'd go with the simplest just single user.
So I need to create a user name for this database.
I'm just going to call it Colt password.
I'm just going to be rusty
so technically you guys would be able to connect to this database because I am showing you this part
.
You would be able to connect to resti her to to this database using my username and password which is
just for the database.
Again that's not the username and password for my account on Monga Lab. It's just for that database
.
But you could go in and screw with you know whatever super important data I have on Joachim.
But I am going to show it to you just so that you see how it works.
All right so quick create.
So now we're going to go back.
You can copy this if you didn't copy this URL go back here and we need to update this so instead of
DB user you want colt and instead of D-B password we want to pass through that set which is Rustie whatever
password and username you set there is what you need to use.
So I'm going to go ahead and comment on the original line out that is using our local database.
I'm going to copy this r URL and use that to connect to.
Now I'm not getting rid of this in the next video I'm going to talk about why we don't want to just
use the same database for development purposes and production.
We'll talk about that right now but I'm going to show you a solution in the next video so we can have
multiple databases in this video.
We're just going to get it running locally and on Roku using this Mongul lab the same database but usually
you don't want to do that.
You don't want to worry about screwing up data deleting things corrupting stuff.
You want to keep it separate but we're not doing that just yet.
So let's just try connecting locally.
So I'm going to go back here right now our application with the change we made has nothing to do with
this Man-God process running anymore.
So when we start this up and refresh this page if things went well if our lab database is set up correctly
there will be no campgrounds here that should be empty.
So let's try to make sure in the right directory node APTA genius says the server has started refresh
.
And as you can see we have no campgrounds.
So we are successfully connect to a database because we didn't get an error if we hadn't successfully
corrected connected if you or I was wrong we would get something like error.
Connection refused like we did with Heroku just a few minutes ago and we're not connected to the original
database because we don't see any of that data.
You may not have had the exact same data obviously you might had you might have not had any campground
yet might have had 20 or 30 but in my case it illustrates to you that we're now using this Mangalam
set up.
So let's just test it out make sure that we can still sign up and successfully add new posts and new
campgrounds.
So I'll send up with resti at Tator dot com.
Password will just be password.
So we sign up and we go let's add a new campground.
The name is rodeo beach.
Description is a beach with pretty rocks and I'll use this image that I took last weekend trying that
whole photography thing out.
Obviously use whatever image you like.
Make sure that it successfully creates and it does awesome looks good.
OK.
So we are now adding data to the Mongo lab database and if we actually go over to Mangalam and refresh
.
We should be able to see in collections.
We now have two collections campgrounds and users.
So let's take a look at campgrounds for instance.
We'll take a moment to load but you actually have access to the data here.
You can see it.
So it's pretty cool.
It's a nice interface.
You can do things like delete all documents but I could also add something in here or edit something
if I needed to.
Generally you don't want to be doing that but it is pretty common to want to read the data out.
Great.
So we have it working locally.
Now all we need to do is add that change and push it up to her oku.
So we'll go back stop the server.
We need to add that change in where we replaced the Vango you are all with Mangu D-B Monga lab dot com
slash whatever your particular URL is.
Then we need to do a commit to say add Mongo lab and then get push Heroku master.
So again it goes through a bunch of stuff at re-installs all the packages.
Actually it checks to see if they have been installed already.
It attempts to reinstall them realizes it already has all of them.
Then it's going to run NPM start and fingers crossed all work this time.
You never know with deploying it's always sort of always a little bit of a crapshoot depending on what
kind of things you've been doing with my boot camp students it was always a bunch of students who would
wait until the very last minute to deploy right before a big presentation when companies were coming
in.
And that led to a lot of unexpected errors in students assumed would be straightforward because the
simple app that we deployed took five minutes and it turns out that they ran into some weird error or
something went wrong.
There are no CSSA compilation or something happened and you run into these crazy errors deploying.
So don't underestimate it.
It's not always super super smooth.
Now let's try and refresh this page or this one here.
UPS area sorry that was our old app this one.
Which is your camp.
And let's look at the campgrounds.
And there we go.
So it's connecting to Mongul.
Now remember that this is sharing the data with the local version the development version which is really
not ideal but it's working fine for now.
Great.
So to review all those steps you went through everything was exactly the same.
As far as get a net Heroku create ad commit get push Heroku master.
But the problem was that our Mangu database was running locally and that wouldn't work on Heroku.
So then we went to Mangu lab created a hosted Monga lab database that we can connect to from our local
version and from the Heroku version.
And the next video we're going to see how we can change databases depending on if we're using Heroku
or for writing code locally and development.
All right.