API Design in Node.js, v4

Debugging Routes

Scott Moss

Scott Moss

Superfilter AI
API Design in Node.js, v4

Check out a free preview of the full API Design in Node.js, v4 course

The "Debugging Routes" Lesson is part of the full, API Design in Node.js, v4 course featured in this preview video. Here's what you'd learn in this lesson:

Scott uses Thunder Client to test and debug all the route handlers in the application. Once a user is authenticated, a product is created along with updates for the product.


Transcript from the "Debugging Routes" Lesson

>> Cool. Let's start and stop that again. Let's try to walk through a whole thing of getting a product and then doing a whole update thing, and we'll go from there. I don't know if we'll have time to do the update points, since it's mostly the same anyway, so I want to move on to some more interesting things, but we'll see.

Okay, so let's give it a try. So we're gonna go over here. We are going to, do I have product posts already? I don't. Nope. Okay, yeah, we're gonna do it all here. So I'll say posts to products. Let's create a product here. So for the body a product needs a name and that's about it.

So I'll just say, my car app. We'll try to create that. Cool, so we got a product, created it. I need the ID for that. Someone grabbed that idea that product. And then what I'm gonna do is I'm going to create an update. So I'm gonna say post to update.

And I know update needs for to be created, A title, a body, and a product ID. So I had to pass all those to create an update. So let's do that. So I'm gonna go here. I wanna say title, New Feature this, And body. This is the best feature yet, and then product Id, the thing I just copied.

So we got that, pretty sure it's the same JSON Web Token, this might fail. Let me just make sure it's the same one. Let's copy this it looks the same. I was gonna copy it. Okay, we got that, we got that. Let's see if this works. So hit send and something just straight up died.

So let's see what happens. So this one says Prisma product find unique invocation needs at least one argument that I missed I might not what's that? I switched it to find unique, and I didn't make it unique. So let's go back and do that. And update which, I believe, is in create update.

So yeah, we that unique key. Index thing I guess, so I guess it's just like yeah, I didn't find it. So that's because this should be product Id, not Id. So let's try that. Go back to this one, see if it works this time. It looks like it failed again, this one says unknown arc product Id.

Data for Prisma update creates, okay, let's see. Okay, and that's because I just put the whole erectile body in there. So we can't do that we'll have to do it the old school way. So I'll say title actually we can do we can do a little quick little trick we're going to structure it.

So we'll just say productI.rest) = req.body, like that, and then I can just come down here and say this is product ID. And then, I can just say this is the rest of the stuff. All right, let's run that one more time. Send it, got another error. [LAUGH] Okay, this one says updateAdd is missing, interesting.

Argument product, data.product is missing. That's interesting, okay. So in order to get past that, I guess what you really want to do is actually just change the schema to make updatedAt have default. I guess what I thought update, I thought you didn't have to do that, maybe you do.

But we can also write it in ourselves if we wanted to, but that.
>> I think they have an add updatedAt.
>> That's what I thought. Yeah, yeah, there it is. There we go. Boom! I guess you just needed that. Now you got to run a migration. [LAUGH] Whenever you change the schema, you gotta run a migration.

So let's run the migration. In practice, you'll end up running so many migrations, so, From that, Run that migration there on an update and then, yeah. Yeah, I don't know why he was tripping about the product Id, I know I need a product Id here, so not sure why that was tripping.

Okay that looks fine, let's go back. I'm actually just gonna I don't know why I didn't like the product Id, it needs a product Id, that's interesting. I'm actually gonna do that req.body.productId, just put req.body back again, it needs it. So, yes, question.
>> One thing I found in use in my example is the connect.

You can link the product, and then you can link connect and pass in unique IDs, and then you don't have to do a separate query for the product.
>> There you go. That's actually the other syntax that I was trying to figure out earlier. I was like, there's another way to LinkedIn, and it was connect.

Yeah, connect. Maybe we should do that which I'm just trying to be lazy and like do the whole thing but let's do it. So title, req.body.title, and then you have what is it body like that, body: rew.body. And then, what is other word, it's less as the produtId, what is good to say product to say connect to the actual products here.

>> One limitation of this though, at least from what I was trying out here, seem to be that you have to pass in a unique identifier or a unique identifier you can't do like anything other than that?
>> So you're saying you have a need product idea.
>> It needs an ID, but you can't like also look up belonging to the user.

>> Right. Right, yeah, that's interesting. Okay. Yeah, let's see what happens.
>> Unless, there's another syntax I'm not aware of, but it doesn't seem to be balance.
>> Yeah, it could very well be, but I don't know. Let's see what happens with this one, so send that someone also doesn't like anything we're doing.

This one says unknown name and forgot to change that back. So I think it's like Id. Product.id There we go. Run that again. Okay, we finally got it, trials, multiple trials, multiple errors finally created an update. I hope seeing you struggled through that showed you some insight on how someone else debugs things, which for me was always super helpful, and thanks for the help.

Cool, so yeah, we got that. So we created this update, we can grab this update's ID and let's try to get an update. So now, we can change this to update, change this to get, through the body we don't need it. Now, let's see if we can get an update.

Yeah, so we got the update that works. Well that's getting all the updates actually, which is only one, let's try to get one update, which will be this. That worked, so get all get one works. I guess let's try to update one? And let's try to update the title which used to be this to something else.

So we can say title. New title, and see what happens here. That works title got updated to new title, and then the last thing we wanna do is see, if we can delete this thing. We don't need the body, and that works. We deleted it. If we try to get it one more time, it should be gone, and it is, okay?

So we got the update.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now