Check out a free preview of the full API Design in Node.js (using Express & Mongo) course:
The "Exercise 9 Solution, continued" Lesson is part of the full, API Design in Node.js (using Express & Mongo) course featured in this preview video. Here's what you'd learn in this lesson:

Scott continues the exercise 9 solution by explaining the get method in the postController and the getOne method in the userController.

Get Unlimited Access Now

Transcript from the "Exercise 9 Solution, continued" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Speaker 1: So this is my solution for the get inside the post controller. Because get is just going to get back all the instances of the collection.
>> Speaker 1: But there's a little difference in how post works because it has relations, so we have to change it to slightly different than what's going on with the other ones.

[00:00:23] So what we have to do is we can just call .find. I'm just passing an empty object here but if you just wanna return back every single one, you don't really have to pass anything. I'm just doing this just to be safe. But you don't have to. You could just say just find everything on post.

[00:00:37]
>> Speaker 1: And then I call .populate.
>> Speaker 1: And remember, what populate does is it's gonna go to the fields on this document. Who has the type? Well, first, what it will do. It won't do anything unless you tell it what fields to populate. So when you call .populate. And you give it the fields that you want to populate, it will go to these respective fields on the document.

[00:01:02] And it will grab their object IDs, cuz that's their values. And then it will go to whatever the ref says, and it will go to that collection with the object ID, find it, and attach it to this field name right here. So what I mean by that, if we wrote a post model and we look, I dunno why that's an array, it should not be an array.

[00:01:24] And we look at the author.
>> Speaker 1: And it's ref is user, so inside of here, when we say populate author, what it's doing is, it's gonna go here, it's gonna grab this object ID, and it's like, okay what collection do you belong to? You belong to the user collection.

[00:01:46] Great. Let me go there with that object ID, find the appropriate user, and attach it to this author property. And it's doing the same thing for categories.
>> Speaker 1: And because categories is an array, it'll go inside the array and get every single one of the categories and populate it.

[00:02:08] I don't know why that was backwards. That should be an object, that should've been an array.
>> Speaker 1: And then I have to call .exec on here, because populate doesn't return a promise, so I have to call .exec, and then I just send them all back.
>> Speaker 1: Any questions on this?

[00:02:34] The only difference between this and the rest of them is that we need to call populate. The rest of them are just like this. It's like find them all, send them all back. But because we have to populate, we just wanna populate them. We don't have to populate, it's just we probably want that data.

[00:03:01]
>> Speaker 1: Any questions?
>> Speaker 2: They're asking, so populate is to hydrate the model's relationships?
>> Speaker 1: Yes, that's exactly what it is. That's a good way of putting it at call time. But it won't persist in the database, so remember that. It's not like creating a join table that's gonna be atomic and save in the database.

[00:03:18] It's only doing that when you make the query. It virtually gives it to you. It's not like I'm gonna make it and save it for next time. It's not, it's only gonna do that at call time. So yeah it does. It will hydrate the objects.
>> Speaker 1: Also you might wanna do a population in here, all this inside the params on post, because this is gonna be fired when we want to update a post, delete a post, or get one post.

[00:03:49] So, it might be nice to just populate in here, too. So we probably can do that, so we can just come in here and do the same thing. We can just copy that. And just be like yeah, just populate here, too. Boom. So that by the time we get into the delete or the getOne or the put, it's already populated as well.

[00:04:14]
>> Speaker 1: Any questions on that? All right. And then user controller you have to do the get one I think. Yeah, this one's pretty straight forward. You just have to send back the user. And the reason we can do this, wait, does anybody know why we can just say var user or req.user and send that back?

[00:04:36] Why can we do that?
>> Speaker 1: Anybody wanna guess?
>> Speaker 2: Yeah, cuz we added it in the params function of both.
>> Speaker 1: Yeah, we added it up here. Because we know when you make a call to get one, we know that you have to pass it an ID as a parameter.

[00:04:53] And if you have to pass in a parameter ID then we know it's going to hit this one first. And if it hits this one first then it's going to attach to the rectile user and that's going to call next, which is going to come back down to get one which has rectile user.

[00:05:10] The reason I had you do this first before the routing is I wanted you to think about this without having the routing tell you. Because if you looked at routing it would be easy to figure that stuff out. You're just like this has ID or here's the middleware, it's all in order.

[00:05:20] I wanted you to just look at these functions, and try to figure out what order they're gonna happen by looking at the context. So, you could infer that by saying req.user that something inside of here is attaching req.user. So I just did req.user.
>> Speaker 1: There it is. Whatever is on the left hand side.

[00:05:43] So this is what's happening first.