Return to Video

Express + Mongoose : Blogger, Part 3

  • 0:02 - 0:04
    SHORTY: So we have a form.
  • 0:04 - 0:05
    We can see it.
  • 0:05 - 0:06
    It's right here.
  • 0:06 - 0:08
    For those of you watching the videos,
  • 0:08 - 0:10
    we did a couple of things the CSS
  • 0:10 - 0:12
    while we weren't recording,
  • 0:12 - 0:14
    which moved this inside a little bit.
  • 0:14 - 0:16
    We added actual padding to the body
  • 0:16 - 0:17
    because it looks better.
  • 0:17 - 0:19
    And then we made a page title class
  • 0:19 - 0:22
    which we can apply to h3s everywhere...
  • 0:22 - 0:25
    That give us a little page title, right?
  • 0:25 - 0:27
    We can use an h3 in our views
  • 0:27 - 0:29
    to let us know what's going on
  • 0:29 - 0:30
    on this page.
  • 0:30 - 0:32
    But outside of that,
  • 0:32 - 0:32
    this is it.
  • 0:32 - 0:35
    This is 100% of what we're about to do.
  • 0:35 - 0:37
    We need to now post.
  • 0:37 - 0:41
    Right, so we type some things.
  • 0:41 - 0:42
    And we hit "post it."
  • 0:42 - 0:45
    And we hang.
  • 0:45 - 0:47
    Right?
  • 0:47 - 0:48
    And why did we hang?
  • 0:48 - 0:49
    Well, we hang 'cause we're going
  • 0:49 - 0:54
    inside of our post controller to...?
  • 0:54 - 0:57
    To create.
  • 0:57 - 0:58
    And so theoretically we want to
  • 0:58 - 1:00
    create a post.
  • 1:00 - 1:01
    But we don't have any way
  • 1:01 - 1:04
    to talk to our database, do we?
  • 1:04 - 1:05
    We haven't done that yet.
  • 1:05 - 1:07
    Now what do we want to use?
  • 1:07 - 1:08
    We want to use mongoose.
  • 1:08 - 1:11
    Mongoose is the new sexy, right?
  • 1:11 - 1:12
    So we want to use our new
  • 1:12 - 1:13
    mongoose skills,
  • 1:13 - 1:17
    which means that we need to create a...
  • 1:17 - 1:20
    schema, right?
  • 1:20 - 1:23
    And we need to use that.
  • 1:23 - 1:25
    So how do we do that?
  • 1:27 - 1:29
    How are we going to do it? Well...
  • 1:29 - 1:31
    STUDENT: We need to require?
  • 1:31 - 1:37
    SHORTY: We're going to require post js
  • 1:37 - 1:39
    in our models folder,
  • 1:39 - 1:40
    which is going to have information
  • 1:40 - 1:43
    about our schema and our post object,
  • 1:43 - 1:46
    and a way to talk to, right,
  • 1:46 - 1:47
    the actual database.
  • 1:47 - 1:49
    So we're going to put our schema in here.
  • 1:49 - 1:51
    And we're going to create the model
  • 1:51 - 1:53
    that will actually connect to the database
  • 1:53 - 1:54
    for us in here.
  • 1:54 - 1:56
    And then we are going to module.exports
  • 1:56 - 1:58
    the model.
  • 1:58 - 2:00
    And then everywhere we want to use
  • 2:00 - 2:02
    posts, we just require it.
  • 2:02 - 2:03
    And if we don't require it,
  • 2:03 - 2:04
    we're not using posts.
  • 2:04 - 2:06
    Does that sound nice?
  • 2:06 - 2:09
    STUDENT: Okay. So is it post.js, or posts.js?
  • 2:09 - 2:12
    SHORTY: Ah! That's a great question.
  • 2:12 - 2:16
    Model names are singular.
  • 2:16 - 2:19
    Controller names are plural.
  • 2:19 - 2:20
    STUDENT: Okay.
  • 2:20 - 2:24
    SHORTY: View folder names are plural.
  • 2:26 - 2:29
    This is a structure that you will get used to
  • 2:29 - 2:30
    in time.
  • 2:30 - 2:32
    It is much like restful routing.
  • 2:32 - 2:33
    There has been decisions made
  • 2:33 - 2:35
    by the universe, without your permission
  • 2:35 - 2:38
    as how we should name things.
  • 2:38 - 2:39
    You are too late to the party
  • 2:39 - 2:40
    to change it.
  • 2:40 - 2:41
    So you should accept it,
  • 2:41 - 2:43
    and go with the flow
  • 2:43 - 2:46
    that way everybody around you is happy.
  • 2:46 - 2:49
    So in here, what do we need to do?
  • 2:49 - 2:50
    We actually need to secretly go back
  • 2:50 - 2:52
    to our server.js, and require some of this stuff,
  • 2:52 - 2:54
    don't we?
  • 2:55 - 2:56
    We're going to need, at the very least,
  • 2:56 - 2:59
    mongoose and the mongoose.Schema,
  • 2:59 - 3:00
    yeah?
  • 3:00 - 3:03
    And in some sense, at the very most.
  • 3:03 - 3:07
    This is the entirety of what we need in here.
  • 3:07 - 3:11
    Inside our -- go away -- here.
  • 3:11 - 3:13
    Of our mongoose.
  • 3:13 - 3:15
    Schema.
  • 3:15 - 3:17
    And now what?
  • 3:17 - 3:23
    Var postSchema =
  • 3:23 - 3:25
    schema... I think we can even do it
  • 3:25 - 3:26
    without new.
  • 3:26 - 3:28
    I think we can just -- anything you pass
  • 3:28 - 3:30
    into the Schema will be the same as
  • 3:30 - 3:32
    having called new Schema.
  • 3:32 - 3:35
    'Cause I think what we're going to do next
  • 3:35 - 3:41
    is var Post = mongoose.model,
  • 3:41 - 3:47
    Post, comma, postSchema, right?
  • 3:47 - 3:52
    And then we'll do module.exports = Post.
  • 3:54 - 3:57
    And now, any time we require models post,
  • 3:57 - 3:59
    it will return to us, the Post object
  • 3:59 - 4:00
    so we can now do things like
  • 4:00 - 4:02
    post.find anywhere,
  • 4:02 - 4:03
    post.delete,
  • 4:03 - 4:04
    post.this,
  • 4:04 - 4:06
    post.whatever.
  • 4:06 - 4:08
    Yes? What?
  • 4:08 - 4:09
    MATT: Can you remove schema and add a [additional?] server [inaudible]...?
  • 4:14 - 4:16
    SHORTY: Yeah, I don't think we need it there anymore.
  • 4:16 - 4:18
    Yeah, as long as none of the routes
  • 4:18 - 4:21
    defined there actually need anything.
  • 4:21 - 4:22
    So Matt's absolutely right.
  • 4:22 - 4:24
    Since we're never going to use mongoose
  • 4:24 - 4:29
    during the course of this server.js file...
  • 4:34 - 4:35
    Oh, shoot.
  • 4:35 - 4:39
    We can get rid of the schema.
  • 4:41 - 4:43
    But we do need mongoose because we still
  • 4:43 - 4:45
    are running mongoose.connect.
  • 4:45 - 4:46
    We don't need schema in there.
  • 4:46 - 4:48
    That's fine.
  • 4:48 - 4:49
    That was a good thought.
  • 4:49 - 4:51
    And I forgot that we had to connect
  • 4:51 - 4:53
    to the database.
  • 4:53 - 4:54
    So yeah...
  • 4:54 - 4:56
    So inside post,
  • 4:56 - 4:58
    what should our posts be made up of?
  • 4:58 - 4:59
    We already said the three things.
  • 4:59 - 5:01
    They're in our form.
  • 5:01 - 5:03
    Author, which it is a what?
  • 5:03 - 5:04
    STUDENT: By line?
  • 5:04 - 5:05
    STUDENT: String.
  • 5:05 - 5:12
    SHORTY: String? By line? What did I call it there?
  • 5:12 - 5:14
    No, that is the author.
  • 5:14 - 5:15
    By line is the author.
  • 5:15 - 5:17
    Title is a string.
  • 5:17 - 5:20
    And content,
  • 5:20 - 5:24
    is a string.
  • 5:24 - 5:25
    Now these are -- this is an
  • 5:25 - 5:28
    unfancy schema, right?
  • 5:28 - 5:31
    I have not specified anything is required.
  • 5:31 - 5:34
    I have not specified anything is
  • 5:34 - 5:36
    unique, or anything like that.
  • 5:36 - 5:37
    It would be kind of weird to have
  • 5:37 - 5:39
    anything in this to be unique.
  • 5:39 - 5:43
    Each author can have one post, right?
  • 5:43 - 5:44
    If you try to post under the
  • 5:44 - 5:45
    same author name twice,
  • 5:45 - 5:46
    you're going to have to start
  • 5:46 - 5:49
    adding the third, the fourth, the fifth.
  • 5:49 - 5:50
    The same title.
  • 5:50 - 5:51
    Everybody's going to like, do something about
  • 5:51 - 5:55
    their trip to New York or whatever, right?
  • 5:55 - 5:59
    And content being unique, while nice
  • 5:59 - 6:04
    for the readers is not necessary, right?
  • 6:04 - 6:06
    Are we good?
  • 6:07 - 6:09
    So far we have our postSchema,
  • 6:09 - 6:12
    and then we have our post model.
  • 6:12 - 6:16
    And then we export the post model.
  • 6:16 - 6:17
    That's it.
  • 6:17 - 6:18
    Now anywhere we require this,
  • 6:18 - 6:19
    we can use it.
  • 6:19 - 6:20
    So where should we probably
  • 6:20 - 6:21
    require it?
  • 6:21 - 6:25
    Inside the post, right?
  • 6:25 - 6:27
    It was the controller.
  • 6:27 - 6:29
    Inside the post controller, right?
  • 6:29 - 6:32
    Theoretically in here,
  • 6:32 - 6:33
    we're going to want to actually
  • 6:33 - 6:37
    access the database.
  • 6:40 - 6:43
    Here's where things get hairy.
  • 6:43 - 6:46
    I think I want to do this.
  • 6:50 - 6:51
    I think.
  • 6:51 - 6:52
    STUDENT: They're models.
  • 6:52 - 6:53
    SHORTY: Oh, yeah, I'm sorry.
  • 6:53 - 6:54
    I'm just --
  • 6:54 - 6:56
    Ah, shoot.
  • 6:59 - 7:04
    I want to import the post model, right?
  • 7:04 - 7:05
    If I don't have this, how am I
  • 7:05 - 7:08
    going to do anything?
  • 7:09 - 7:11
    Watch.
  • 7:13 - 7:14
    What?
  • 7:14 - 7:21
    New Post, req.body dot...?
  • 7:22 - 7:24
    All right, let go back and
  • 7:24 - 7:26
    look at the form again.
  • 7:26 - 7:28
    Everything is inside what name space?
  • 7:28 - 7:29
    STUDENT: Post.
  • 7:29 - 7:30
    SHORTY: Post. So it's gonna --
  • 7:30 - 7:35
    when body-parser does our parsing,
  • 7:35 - 7:37
    what's going to happen?
  • 7:37 - 7:39
    STUDENT: It's going to pull all the data
  • 7:39 - 7:40
    from the code into the post object?
  • 7:40 - 7:44
    SHORTY: Yup. And it's going to put it in an object whose...
  • 7:44 - 7:48
    So inside req.body, we're going to have a
  • 7:48 - 7:53
    top-level attribute called post.
  • 7:53 - 7:55
    And then it's going to have three attributes,
  • 7:55 - 7:59
    which are author, title, and content.
  • 7:59 - 8:03
    So to get to it, I need to .post.
  • 8:03 - 8:08
    So here, req.body.post.
  • 8:08 - 8:10
    Console.log(newPost).
  • 8:10 - 8:11
    This is going to let us know
  • 8:11 - 8:14
    something happened.
  • 8:14 - 8:15
    Now let's go back and look at my schema.
  • 8:15 - 8:20
    I want to make sure I get author, title, content.
  • 8:20 - 8:22
    Author, title, content.
  • 8:22 - 8:24
    That's really important, right?
  • 8:24 - 8:25
    It's going to flail about
  • 8:25 - 8:28
    if it's not matching.
  • 8:28 - 8:29
    STUDENT: Do they have to be in the same order?
  • 8:29 - 8:31
    SHORTY: No, absolutely not.
  • 8:31 - 8:33
    These are just named properties.
  • 8:33 - 8:34
    Objects don't care.
  • 8:34 - 8:35
    They're not arrays.
  • 8:35 - 8:36
    They're in almost like arrays
  • 8:36 - 8:37
    but they're not arrays.
  • 8:37 - 8:38
    Yes?
  • 8:38 - 8:40
    STUDENT: In the object where it says post
  • 8:40 - 8:45
    that's where it would create questionable posts?
  • 8:45 - 8:48
    SHORTY: Yes. Yes.
  • 8:50 - 8:53
    STUDENT: So I was -- I got most of it working last night
  • 8:53 - 8:57
    but if you put a number, or maybe more like a string
  • 8:57 - 9:00
    in something that you define as a number,
  • 9:00 - 9:01
    would it wig out a little?
  • 9:01 - 9:02
    SHORTY: It does wig out.
  • 9:02 - 9:03
    It will throws errors, won't it?
  • 9:03 - 9:06
    STUDENT: Right, yeah. Because I didn't do too much testing on
  • 9:06 - 9:08
    what it was storing or anything.
  • 9:08 - 9:10
    SHORTY: Yeah, so you might want to do is
  • 9:10 - 9:12
    in your form, actually specify that it's
  • 9:12 - 9:14
    a number field.
  • 9:14 - 9:18
    And it will serialize as a number.
  • 9:21 - 9:23
    How are we doing?
  • 9:23 - 9:26
    This is super simple at this point, right?
  • 9:26 - 9:29
    So all I'm doing is I'm importing
  • 9:29 - 9:32
    the ability to access the database
  • 9:32 - 9:33
    and make posts.
  • 9:33 - 9:35
    I'm using the mongoose model as a
  • 9:35 - 9:37
    way to do that.
  • 9:37 - 9:38
    When this form submits,
  • 9:38 - 9:40
    it should fire off into the post
  • 9:40 - 9:42
    controller in here.
  • 9:42 - 9:46
    It should use the post, which I required
  • 9:46 - 9:47
    and make a new one,
  • 9:47 - 9:50
    and then just log it.
  • 9:50 - 9:54
    This is intense.
  • 9:54 - 9:55
    Start everything up.
  • 9:55 - 9:56
    Nothing broke yet.
  • 9:56 - 10:01
    But I haven't hit a thing yet.
  • 10:01 - 10:05
    God, I'm so happy with this form!
  • 10:11 - 10:13
    STUDENT: Aww!
  • 10:16 - 10:17
    STUDENT: Yay!
  • 10:17 - 10:19
    SHORTY: Oh-ho-ho!
  • 10:19 - 10:20
    Is it there?
  • 10:20 - 10:21
    STUDENT: Yes.
  • 10:21 - 10:23
    SHORTY: How happy are you on a scale
  • 10:23 - 10:24
    of 1-5?
  • 10:24 - 10:25
    STUDENT: Ten.
  • 10:25 - 10:26
    SHORTY: Oh, 10?
  • 10:26 - 10:27
    I like 10.
  • 10:27 - 10:29
    That's pretty cool, right?
  • 10:29 - 10:30
    Everything works the way we want it
  • 10:30 - 10:31
    to work.
  • 10:31 - 10:34
    So now, let's not just make the model
  • 10:34 - 10:35
    let's also...
  • 10:35 - 10:36
    STUDENT: Store it?
  • 10:36 - 10:37
    SHORTY: Store it, which is --
  • 10:37 - 10:40
    we save it, right?
  • 10:40 - 10:45
    So newPost = new Post.
  • 10:46 - 10:49
    NewPost.save, which then takes a
  • 10:49 - 10:52
    callback function, which gives us
  • 10:52 - 10:56
    an error and the post itself, right?
  • 10:56 - 10:59
    So when you call methods like
  • 10:59 - 11:02
    save, or delete, or whatever,
  • 11:02 - 11:04
    usually they have a callback function
  • 11:04 - 11:05
    that gets executed after the
  • 11:05 - 11:07
    database is done.
  • 11:07 - 11:09
    And the reason is, because you don't want to
  • 11:09 - 11:10
    keep doing things while you're waiting
  • 11:10 - 11:13
    for the database to finish, right?
  • 11:13 - 11:14
    You're like, hey, database, make a bunch of
  • 11:14 - 11:16
    changes and now I'm going to ask you for some
  • 11:16 - 11:18
    things in the meanwhile.
  • 11:18 - 11:20
    That's really bad.
  • 11:20 - 11:21
    We want to, like, kind of close down
  • 11:21 - 11:22
    access to the database
  • 11:22 - 11:24
    until this finishes.
  • 11:24 - 11:26
    So we're going to save it.
  • 11:26 - 11:28
    It's going to take a callback function.
  • 11:28 - 11:30
    Mongo callback functions are almost exclusively,
  • 11:30 - 11:35
    error something.
  • 11:35 - 11:36
    So we could do something like this.
  • 11:36 - 11:42
    If error, and run some code.
  • 11:42 - 11:44
    Log the error.
  • 11:44 - 11:46
    That seems like useful stuff, right?
  • 11:46 - 11:47
    Else...
  • 11:47 - 11:49
    Okay. So now, if there wasn't an error,
  • 11:49 - 11:51
    and the post saved correctly,
  • 11:51 - 11:55
    what should we do?
  • 11:55 - 11:56
    STUDENT: Console.log?
  • 11:56 - 11:57
    STUDENT: Maybe say success?
  • 11:57 - 11:58
    SHORTY: Maybe say success.
  • 11:58 - 12:01
    What actually is a useful success function here?
  • 12:01 - 12:03
    Let's do something to the response, right?
  • 12:03 - 12:04
    STUDENT: Render it.
  • 12:04 - 12:07
    SHORTY: Res.render. No... remember
  • 12:07 - 12:10
    inside the actions that tickle the database,
  • 12:10 - 12:13
    we actually want to always...?
  • 12:13 - 12:15
    It rhymes with "she direct."
  • 12:15 - 12:16
    STUDENT: Redirect?
  • 12:16 - 12:20
    SHORTY: Rediresct. Res.redirect to somewhere.
  • 12:20 - 12:22
    I don't know where yet.
  • 12:22 - 12:25
    Let's just redirect back to new.
  • 12:25 - 12:26
    Okay?
  • 12:26 - 12:29
    Here, again, this doesn't know
  • 12:29 - 12:31
    anything about the agreement with the
  • 12:31 - 12:33
    router.
  • 12:33 - 12:35
    So we gotta give it a full path.
  • 12:35 - 12:36
    The agreement with the router
  • 12:36 - 12:39
    only gives us shortcuts to these
  • 12:39 - 12:43
    router.get strings.
  • 12:43 - 12:43
    Okay?
  • 12:43 - 12:45
    Everywhere else we have to use
  • 12:45 - 12:46
    full strings.
  • 12:46 - 12:47
    I have no idea.
  • 12:47 - 12:49
    For some reason, I have this weird thing
  • 12:49 - 12:51
    where I use double quotes,
  • 12:51 - 12:53
    whenever it's the second thing.
  • 12:53 - 12:54
    I don't know why.
  • 12:54 - 12:55
    It's really weird.
  • 12:55 - 12:56
    Always --
  • 12:56 - 13:01
    I can't stop myself.
  • 13:01 - 13:04
    So there's a philosophy in quoted strings
  • 13:04 - 13:06
    where you use single quotes when they're
  • 13:06 - 13:07
    only being used internally.
  • 13:07 - 13:08
    And you use double quotes to indicate
  • 13:08 - 13:12
    it's going to go to the user somehow.
  • 13:12 - 13:14
    STUDENT: That's cool.
  • 13:14 - 13:15
    SHORTY: It's a philosophy.
  • 13:15 - 13:16
    It doesn't really matter.
  • 13:16 - 13:17
    As it turns out,
  • 13:17 - 13:18
    it doesn't matter.
  • 13:18 - 13:19
    Not even a little bit.
  • 13:19 - 13:22
    But I like that philosophy.
  • 13:22 - 13:23
    I like that separation.
  • 13:23 - 13:24
    It makes sense to me.
  • 13:24 - 13:25
    STUDENT: Wait, what is it again?
  • 13:25 - 13:26
    SHORTY: Double quotes are when you're
  • 13:26 - 13:27
    talking to the user.
  • 13:27 - 13:28
    So any string which is going to be used
  • 13:28 - 13:30
    to eventually be sent back to the user,
  • 13:30 - 13:31
    put that in double quotes.
  • 13:31 - 13:33
    Anything which is only ever going to be
  • 13:33 - 13:35
    used internally, put single quotes.
  • 13:35 - 13:37
    It's like a nice idea on when you're like, oh, shoot, this stuff's
  • 13:37 - 13:40
    going back, I have to make sure it's okay.
  • 13:40 - 13:44
    whereas this is never seen by the user.
  • 13:44 - 13:46
    And then we get into philosophical discussions...
  • 13:46 - 13:47
    since it's rerouting, is that really
  • 13:47 - 13:48
    seen by the user?
  • 13:48 - 13:51
    Let's ignore that.
  • 13:51 - 13:52
    Right?
  • 13:52 - 13:54
    So let's kill that.
  • 13:54 - 13:55
    Oh, man.
  • 13:55 - 13:56
    I'm so nervous.
  • 13:56 - 13:59
    Start it up.
  • 13:59 - 14:01
    Go back to posts/new.
  • 14:01 - 14:03
    I'm just going to hit post.
  • 14:03 - 14:05
    Boop, we went back to posts/new.
  • 14:05 - 14:07
    Which meant two things.
  • 14:07 - 14:10
    One, the error didn't happen.
  • 14:10 - 14:12
    Which probably means, now, in our database
  • 14:12 - 14:13
    there's a...?
  • 14:13 - 14:14
    STUDENT: Post.
  • 14:14 - 14:18
    SHORTY: Post! Post.save fired off, right?
  • 14:18 - 14:20
    That's great.
  • 14:20 - 14:20
    What?
  • 14:20 - 14:22
    STUDENT: Okay. I think this is more of an
  • 14:22 - 14:24
    advanced question but, like, in real life,
  • 14:24 - 14:26
    when you do a post, there's a lag
  • 14:26 - 14:28
    between your post and to the database
  • 14:28 - 14:31
    and then you go back to the new page, right?
  • 14:31 - 14:32
    Is there only --
  • 14:32 - 14:35
    is it our jobs as developers to set up that lag?
  • 14:35 - 14:37
    SHORTY: Why would you want a lag?
  • 14:37 - 14:39
    STUDENT: Yeah, that's not --
  • 14:39 - 14:40
    SHORTY: Yeah, that's just a matter of
  • 14:40 - 14:43
    it taking a long time to reget, whatever's
  • 14:43 - 14:44
    going to be rendered on the --
  • 14:44 - 14:46
    at this point our database is so small,
  • 14:46 - 14:49
    there's exactly one entry,
  • 14:49 - 14:50
    that we don't have to worry about it.
  • 14:50 - 14:51
    STUDENT: [Inaudible]?
  • 14:51 - 14:53
    SHORTY: No, 'cause I've only saved once.
  • 14:53 - 14:54
    The first time, I didn't save.
  • 14:54 - 14:55
    STUDENT: Oh.
  • 14:55 - 14:57
    SHORTY: I just logged.
  • 14:57 - 14:58
    STUDENT: We don't need to implement a code
  • 14:58 - 14:59
    for the lagging?
  • 14:59 - 15:00
    SHORTY: Absolutely not.
  • 15:00 - 15:01
    All right.
  • 15:01 - 15:02
    So let's make our index page because that's
  • 15:02 - 15:06
    really where we want to go, right?
  • 15:06 - 15:08
    STUDENT: I use create to do that.
  • 15:08 - 15:12
    SHORTY: Create is new plus save simultaneously.
  • 15:12 - 15:15
    Same thing. I personally prefer doing new
  • 15:15 - 15:17
    separate from save.
  • 15:17 - 15:19
    STUDENT: Yeah, well, create I -- [Inaudible].
  • 15:19 - 15:21
    SHORTY: Yes. I think that that's fine.
  • 15:21 - 15:24
    But I actually really like separating the two.
  • 15:24 - 15:25
    I like making the new thing.
  • 15:25 - 15:28
    And then saving the thing separately.
  • 15:28 - 15:30
    That's just the way I've always done it.
  • 15:30 - 15:32
    That's --
  • 15:32 - 15:33
    STUDENT: I thought, I mean, I was just talking
  • 15:33 - 15:35
    if you were in an array, I was just trying to
  • 15:35 - 15:37
    just get it to work.
  • 15:37 - 15:40
    MATT: Yeah. So this, I mean, this is totally
  • 15:40 - 15:42
    a preferential thing.
  • 15:42 - 15:44
    How you've done it from with day one
  • 15:44 - 15:46
    or if you do it Matt's way,
  • 15:46 - 15:48
    the only difference that I notice
  • 15:48 - 15:50
    is that you can't pass in an array to create.
  • 15:50 - 15:54
    So if you have multiple objects,
  • 15:54 - 15:55
    it will go through and create them all.
  • 15:55 - 15:57
    SHORTY: That you want to do new or in create?
  • 15:57 - 15:58
    MATT: Do create if you want to pass in.
  • 15:58 - 16:00
    SHORTY: Ah, so you were trying to create
  • 16:00 - 16:01
    multiple things.
  • 16:01 - 16:02
    MATT: Yeah, but that's the only difference...
  • 16:02 - 16:06
    SHORTY: I see. That makes sense a lot of sense.
  • 16:06 - 16:07
    STUDENT: All right. Makes more sense.
  • 16:07 - 16:09
    SHORTY: So we're kind of at the point
  • 16:09 - 16:10
    where eventually I will release you
  • 16:10 - 16:11
    to the universe.
  • 16:11 - 16:13
    I don't think we have to build out everything.
  • 16:13 - 16:14
    But do you guys see where
  • 16:14 - 16:16
    mongoose goes?
  • 16:16 - 16:18
    Do you see how to compartmentalize
  • 16:18 - 16:20
    our application?
  • 16:20 - 16:23
    Our application actually now has things
  • 16:23 - 16:26
    in really nice subdirectories where they're
  • 16:26 - 16:28
    only being used when we need them.
  • 16:28 - 16:30
    Does that make a lot of sense?
  • 16:30 - 16:30
    Yes?
  • 16:30 - 16:33
    Let's do our index.
  • 16:33 - 16:35
    So now, we're going to want to
  • 16:35 - 16:40
    post dot... it rhymes with "mind."
  • 16:40 - 16:41
    STUDENT: Find.
  • 16:41 - 16:44
    SHORTY: Find. And what are we going to find?
  • 16:44 - 16:48
    Everything, right?
  • 16:48 - 16:49
    And that gets a callback function, which
  • 16:49 - 16:51
    gets an error, and...?
  • 16:51 - 16:53
    The collection of stuff.
  • 16:53 - 16:54
    The whole collection.
  • 16:54 - 16:55
    Whatever we've found.
  • 16:55 - 16:59
    Why don't we call that posts?
  • 16:59 - 17:01
    Is anybody weirded out by my
  • 17:01 - 17:03
    choice of variable name?
  • 17:03 - 17:04
    All right. That's just where the array of
  • 17:04 - 17:06
    post is going to go.
  • 17:06 - 17:12
    And in here, if err, log err.
  • 17:12 - 17:16
    Otherwise -- and this is very
  • 17:16 - 17:17
    common behavior.
  • 17:17 - 17:18
    Check to see if there's an error.
  • 17:18 - 17:20
    If there is, don't progress.
  • 17:20 - 17:22
    Just stop.
  • 17:22 - 17:23
    And if there isn't an error,
  • 17:23 - 17:26
    then do the required behavior.
  • 17:26 - 17:28
    Else...
  • 17:28 - 17:30
    we probably want to render something, right?
  • 17:30 - 17:39
    Res.render('posts/index')
  • 17:39 - 17:42
    and we should pass it what object?
  • 17:42 - 17:43
    STUDENT: Post object?
  • 17:43 - 17:46
    SHORTY: Ah! That's a real good question.
  • 17:46 - 17:47
    Maybe?
  • 17:47 - 17:50
    Posts is probably what type of object?
  • 17:50 - 17:52
    KRISTYN: So it's just the array that we're going to be...?
  • 17:52 - 17:56
    SHORTY: Posts is actually an array, isn't it?
  • 17:56 - 17:58
    STUDENT: So the client or user --
  • 17:58 - 18:00
    PostsArray...
  • 18:00 - 18:02
    Because it is an array,
  • 18:02 - 18:03
    right?
  • 18:03 - 18:06
    when we use find, it gets a collection of stuff.
  • 18:06 - 18:10
    A collection's default container is a...?
  • 18:10 - 18:10
    Array.
  • 18:10 - 18:11
    When we want to store a bunch of things
  • 18:11 - 18:13
    that are all the same kind of thing,
  • 18:13 - 18:14
    we put them in an array, right?
  • 18:14 - 18:16
    An array is an box that can store things.
  • 18:16 - 18:19
    An object requires named properties.
  • 18:19 - 18:20
    If I get back 20 posts,
  • 18:20 - 18:23
    what's going to be the name for each post,
  • 18:23 - 18:25
    if I've stored it in an object?
  • 18:25 - 18:26
    I don't know.
  • 18:26 - 18:27
    There wouldn't be one.
  • 18:27 - 18:30
    So let's call this posts array.
  • 18:30 - 18:32
    So we know what Matt's about to do
  • 18:32 - 18:34
    isn't insane.
  • 18:34 - 18:37
    I need to make a literal object
  • 18:37 - 18:42
    where the posts are the postsArray.
  • 18:42 - 18:46
    So now inside my ejs file,
  • 18:46 - 18:49
    posts is now a variable.
  • 18:49 - 18:52
    and it points to the array of posts.
  • 18:52 - 18:55
    As we want it to.
  • 18:55 - 18:57
    So remember, often, you'll have to create
  • 18:57 - 18:59
    an object on the spot
  • 18:59 - 19:00
    when you're rendering.
  • 19:00 - 19:03
    That actually has the name you care to use
  • 19:03 - 19:05
    pointing at the actual stop.
  • 19:05 - 19:07
    Does that make sense?
  • 19:07 - 19:08
    I feel like I'm almost at the edge
  • 19:08 - 19:12
    of your ability to pay attention at this point.
  • 19:12 - 19:13
    So let's wrap up
  • 19:13 - 19:16
    and actually render an index page.
  • 19:16 - 19:19
    So for now, this isn't going to do anything, is it?
  • 19:19 - 19:21
    Oh, also...
  • 19:21 - 19:24
    Let's redirect to posts instead of post new.
  • 19:24 - 19:26
    So we can just see the list of
  • 19:26 - 19:28
    all posts, yes?
  • 19:28 - 19:30
    Sarah!
  • 19:30 - 19:33
    Welcome to our recording.
  • 19:33 - 19:35
    How many staircases do we have?
  • 19:35 - 19:37
    Not too many yet, right?
  • 19:37 - 19:39
    STUDENT: Three?
  • 19:39 - 19:42
    SHORTY: Yeah. One, two, three steps?
  • 19:42 - 19:44
    So you're up -- I don't know.
  • 19:44 - 19:46
    You're up on the third-floor landing.
  • 19:46 - 19:48
    It's awful.
  • 19:48 - 19:51
    It's a walk-up.
  • 19:51 - 19:53
    We need to make our index file, right?
  • 19:53 - 19:55
    So in here, we have access to a
  • 19:55 - 19:57
    thing called...
  • 19:57 - 20:00
    Posts, right?
  • 20:00 - 20:05
    Dot, it's an array, forEach,
  • 20:05 - 20:06
    takes a callback function,
  • 20:06 - 20:08
    which grabs a post,
  • 20:08 - 20:13
    and its index.
  • 20:13 - 20:14
    And then we gotta wrap all this
  • 20:14 - 20:18
    nonsense in clown hats.
  • 20:18 - 20:21
    Scary clown hats.
  • 20:21 - 20:23
    So far so good, right?
  • 20:23 - 20:27
    KRISTYN: Is this all going into the body?
  • 20:27 - 20:28
    SHORTY: Yes.
  • 20:28 - 20:30
    KRISTYN: Do you have to do anything to get
  • 20:30 - 20:34
    it to recognize this ejs file or...?
  • 20:34 - 20:38
    SHORTY: So when I call inside my post controller,
  • 20:38 - 20:39
    render(posts/index),
  • 20:39 - 20:42
    it's going to go into posts/index.ejs
  • 20:42 - 20:45
    it's going to run this code.
  • 20:45 - 20:46
    It will finish compiling this.
  • 20:46 - 20:48
    And it will treat it like this text.
  • 20:48 - 20:49
    It makes, like, a string, right?
  • 20:49 - 20:51
    It then takes that string,
  • 20:51 - 20:54
    goes into layout.ejs,
  • 20:54 - 20:57
    and then puts it right here.
  • 20:57 - 21:02
    So why don't we add our h3
  • 21:02 - 21:05
    class = "title."
  • 21:05 - 21:08
    Ah, shoot.
  • 21:08 - 21:12
    Page title.
  • 21:20 - 21:24
    All the posts.
  • 21:24 - 21:26
    Does that sound good?
  • 21:26 - 21:28
    And we're going to iterate over the
  • 21:28 - 21:33
    post collection and render them.
  • 21:33 - 21:35
    Yeah?
  • 21:35 - 21:36
    Let's put them in, I don't know,
  • 21:36 - 21:42
    a div whose class is post.
  • 21:42 - 21:45
    So I can style it.
  • 21:45 - 21:49
    And then in there, have a header.
  • 21:49 - 21:55
    Which is the post title.
  • 21:55 - 22:00
    And then, in a small tag...
  • 22:00 - 22:02
    we'll have, like, by...
  • 22:02 - 22:03
    STUDENT: Put something.
  • 22:03 - 22:06
    SHORTY: Yeah, by...
  • 22:06 - 22:12
    Post_author.
  • 22:12 - 22:14
    And then end my small tag.
  • 22:14 - 22:16
    And end my h4.
  • 22:16 - 22:19
    And then here, in a paragraph,
  • 22:19 - 22:23
    we'll put the post.content.
  • 22:23 - 22:26
    And my paragraph tag.
  • 22:26 - 22:28
    Who feels that this is making sense --
  • 22:28 - 22:30
    the way that we're doing this, right?
  • 22:30 - 22:34
    Now, in some sense, this is a repeatable thing.
  • 22:34 - 22:38
    maybe later in a different part of our application
  • 22:38 - 22:39
    we're also going to want to render
  • 22:39 - 22:41
    our post.
  • 22:41 - 22:43
    Like, for example, in show.
  • 22:43 - 22:44
    If you're going to render it the same way
  • 22:44 - 22:47
    in both, in some sense, this can be
  • 22:47 - 22:48
    pushed into yet another
  • 22:48 - 22:53
    ejs file, which we call a partial.
  • 22:53 - 22:56
    A partial is just a part of a page
  • 22:56 - 22:57
    which is going to be used in
  • 22:57 - 22:59
    multiple places.
  • 22:59 - 23:01
    Really, what's the difference between the
  • 23:01 - 23:06
    new post form and the edit post form?
  • 23:06 - 23:07
    STUDENT: The existence of content?
  • 23:07 - 23:10
    SHORTY: The existence of values, right?
  • 23:10 - 23:12
    Those two forms can be encapsulated also
  • 23:12 - 23:17
    into a partial and things like that.
  • 23:17 - 23:19
    So what we want to do is we want to think about
  • 23:19 - 23:21
    every time we're doing something,
  • 23:21 - 23:24
    how can I do this just a little bit better?
  • 23:24 - 23:26
    How can I reuse things more often?
  • 23:26 - 23:27
    But for now this is good.
  • 23:27 - 23:28
    I don't really care.
  • 23:28 - 23:29
    We're just going to have a bunch of divs
  • 23:29 - 23:31
    with our posts.
  • 23:31 - 23:34
    Let's go quickly style that.
  • 23:34 - 23:37
    I want the post div to be --
  • 23:37 - 23:41
    to have a border.
  • 23:41 - 23:42
    I want that border to have a
  • 23:42 - 23:46
    little bit of a radius.
  • 23:46 - 23:48
    Right, be a little curved.
  • 23:48 - 23:50
    I want it to have some padding of an
  • 23:50 - 23:54
    em, so the post -- everything appears inside of it.
  • 23:54 - 23:57
    And that's just going wrap around my posts
  • 23:57 - 23:58
    and that's all I'm going to do.
  • 23:58 - 24:00
    That's the entirety of my styling.
  • 24:00 - 24:01
    Does that sound good for now?
  • 24:01 - 24:02
    STUDENT: Mm-hmm.
  • 24:02 - 24:04
    SHORTY: Okay. That way -- oh, maybe I want
  • 24:04 - 24:07
    everyone -- so maybe I want, like, a
  • 24:07 - 24:13
    post plus a post to have a margin top.
  • 24:13 - 24:15
    So do you guys remember how
  • 24:15 - 24:16
    this works?
  • 24:16 - 24:21
    If you have anything plus anything,
  • 24:21 - 24:24
    they have to be siblings of each other,
  • 24:24 - 24:25
    which means they have to be
  • 24:25 - 24:28
    at the same level in your dom tree
  • 24:28 - 24:31
    so we're going to render, like, 30 posts, right?
  • 24:31 - 24:34
    I want any post, which has a post before it
  • 24:34 - 24:37
    to have a little margin above it.
  • 24:37 - 24:38
    That way the two posts
  • 24:38 - 24:43
    aren't kissing each other.
  • 24:43 - 24:44
    Like that.
  • 24:44 - 24:48
    So this is everything but the first post
  • 24:48 - 24:50
    is going to get a top margin.
  • 24:50 - 24:51
    And does that make sense why
  • 24:51 - 24:55
    we don't want the top one to have?
  • 24:55 - 24:57
    It should just be at the top.
  • 24:57 - 24:58
    All right.
  • 24:58 - 25:00
    We're almost done.
  • 25:00 - 25:02
    Let's restart our server.
  • 25:02 - 25:05
    Well, let's just make another new post.
  • 25:05 - 25:08
    That way we'll have two to show, right?
  • 25:08 - 25:10
    Who's the author name on this post?
  • 25:10 - 25:10
    STUDENT: Jerry.
  • 25:10 - 25:12
    SHORTY: Jerry, what's your title of your post?
  • 25:12 - 25:15
    STUDENT: Hello.
  • 25:15 - 25:17
    SHORTY: Hello. What's up, Jerry?
  • 25:17 - 25:19
    Remember, you care.
  • 25:19 - 25:21
    STUDENT: I did not get my pizza yet.
  • 25:21 - 25:24
    SHORTY: I did not get my pizza yet.
  • 25:26 - 25:27
    Post it.
  • 25:27 - 25:29
    And here is all of our posts.
  • 25:29 - 25:31
    Who is excited?
  • 25:31 - 25:32
    [ Applause ]
  • 25:32 - 25:34
    All right!
  • 25:34 - 25:36
    We have a perfectly functional --
  • 25:36 - 25:37
    well, we have a boring website.
  • 25:37 - 25:38
    There's still not all of the
  • 25:38 - 25:40
    functionality.
  • 25:40 - 25:41
    But I'm going to leave that to you.
  • 25:41 - 25:43
    I really want to stop here.
  • 25:43 - 25:45
    So let me stop this.
Title:
Express + Mongoose : Blogger, Part 3
Description:

more » « less
Video Language:
English
Duration:
25:45

English subtitles

Revisions