Check out a free preview of the full API Design in Node.js, v3 course:
The "Create a Document Solution" Lesson is part of the full, API Design in Node.js, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Scott live codes the solution to the tests where a document is created by an authenticated user.

Get Unlimited Access Now

Transcript from the "Create a Document Solution" Lesson

[00:00:00]
>> Scott Moss: So for a createOne, this one creates a new document. Okay, so, and it says the createdBy should be the authenticated user. So it's gonna create a new document. And the createdBy filled on that document should be the authenticated user. So you have to keep that in mind.

[00:00:20] So what I'll do, is I'll say, doc = await model.create, because remember, that's how we create what current model is, that create method. And then, we pass in the body, or whatever is being posted to our server, cuz the create one's gonna be used on a post request.

[00:00:39] And we know on a post request, we're gonna be getting information on some type of rec.body, which is what we did earlier on the day. So if I say I wanna create based on whatever the rec.body is, that's halfway there. Because whatever's being sent up to the server, that's definitely what we wanna create.

[00:00:54] But, we also wanna use the authenticated user's created by, which is not going to be on this payload. So instead, what we'll do, is we'll just do an object spread here right quick. And we'll say req.body. And then, we need to add createdBy, which is gonna be req.user._id, which is what it's been for the last two.

[00:01:17] So that's gonna create an object with the body. And then, override the createdBy field to be whatever the authenticated user is.
>> Scott Moss: So we'll do that. And then, we'll say rest.status200.jason doc.
>> Scott Moss: Yes?
>> Speaker 2: Once you return your data, your structure again?
>> Scott Moss: Yes. Yes, I will, thank you.

[00:01:53] I just technically posted a question a bit 201, but 200 is good. So now, if I run this test, maybe, my test is asking for a 201, let's see.
>> Speaker 3: I'm unfamiliar with that dot dot dot.
>> Scott Moss: Dot dot dot? So that's an object spread. That's basically the same as merging an object or extending an object.

[00:02:18] That's the same as if I had an object, and I said, if I had a function called extend, that extended that this object, with this objects, with this object. It's the same thing, wherever the furthest to the right gets priority over the left. So whatsoever on the right, is gonna overwrite any fields on the left and return a new object.

[00:02:36] So it's just a shortcut for extend. It's called object spread. But yeah, that's basically it. It doesn't go over cursively. So it's not an emerge, like if you had nested fields, it won't smartly merge those, it'll just override the whole object, so you still need to merge for that.

[00:02:50] But for a flat copy, yeah, dot dot dot. Pretty cool, okay, so let's scroll up.
>> Scott Moss: Yeah, so, it looks like this one, like I said, it's expecting a 201 and not a 200, and yeah, so we'll do that. Let's say 201 for a post request. Cool, so if we scroll up, we can see that those passed.