
No Comments Yet
Be the first to share your thoughts and start the conversation.
Be the first to share your thoughts and start the conversation.
Complete source code available till this point of lesson is available at
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
In this lesson, we focus on creating the answer.model.ts
file to define an answer's structure in TypeScript. The answer model includes properties such as author, question, content, upvotes, downvotes, and timestamp, emphasizing a one-to-many relationship between questions and answers.
00:00:02 Directly following the question model, we have to implement the answer model.
00:00:07 This one right here.
00:00:08 Below the question, we have many answers.
00:00:11 Each answer has the author or the user that created it, the timestamp, its own numbers of upvotes and downvotes, similar to a question,
00:00:20 because we have to know whether an answer is good or not, the content, and I think that's more or less it.
00:00:26 So let's create it by copying our template model and creating a new file called answer.model.ts.
00:00:35 I'll paste the template and I will use the replace by pressing Ctrl or Command F to replace the model with answer.
00:00:43 There we go.
00:00:44 Now we have the I answer and we can start creating its definition.
00:00:50 So let's first say that each answer will have an author of that specific answer, which will simply be a reference to a user.
00:00:58 After that, we're going to have a question as well, because each answer has to be related to a question.
00:01:04 So we can give it a type of object ID pointing to a question and required is set to true.
00:01:10 So this is a major difference in what I've done in the previous versions of this course.
00:01:15 If I go to the question model right here and put it on the side, you'll notice that no longer do we have a list of answers here.
00:01:25 Before, I actually had an array of different answers which were pointing to references of the answer schema.
00:01:34 But now, you can see that there is no arrays.
00:01:37 This right here simply gives us the number of answers.
00:01:40 And even though this is pointing to a question, it's just a singular object.
00:01:45 It's not an array of answers.
00:01:47 That's because it's a one-to-many relationship.
00:01:50 One question can have multiple answers, but one answer can only be attached to one question.
00:01:56 So this is that performance optimization that I've implemented to make sure that by watching this course, you're getting the best production-level learning.
00:02:04 Each answer will also have its contents.
00:02:07 Then we can have the number of upvotes on an answer, which is going to be a type of number.
00:02:14 And it'll be a default of zero.
00:02:17 We can do the same thing for the downvotes.
00:02:19 And finally, the timestamps will be set to true.
00:02:23 And then we can define the type definition.
00:02:26 We can do that by specifying the author of a type object ID.
00:02:32 Question will be the object ID.
00:02:35 The content will be of a type string.
00:02:37 Upvotes will be a number.
00:02:39 And downvotes will be a number as well.
00:02:42 Now check this out.
00:02:43 Because we're using this IAnswer, or this TypeScript interface in this case, I can immediately notice a mistake that I've made while defining those two properties.
00:02:54 I used the types from the imports right here, which are TypeScript types, and not schema types.
00:03:00 So I am entirely missing the schema.types.objectid reference right here, which are two completely different things.
00:03:08 So thank you TypeScript for saving my ass, and fuck you Copilot for suggesting the wrong code.
00:03:14 See, it's never good to trust AI only.
00:03:17 Great.
00:03:18 With that said, our answer model is done.
00:03:20 Great work.