title
Managing Orders with Mongoose | Creating a REST API with Node.js
description
We worked on the products, we added MongoDB and Mongoose - time to now also connect our order routes to the database.
----------
Learn Node.js in our comprehensive 30h+ course: https://acad.link/nodejs
Source Code: https://github.com/academind/node-restful-api-tutorial/tree/07-orders-and-mongodb/api/routes
----------
• You can follow Max on Twitter (@maxedapps).
• You can also find us on Facebook.(https://www.facebook.com/academindchannel/)
• Or visit our Website (https://www.academind.com) and subscribe to our newsletter!
See you in the videos!
detail
{'title': 'Managing Orders with Mongoose | Creating a REST API with Node.js', 'heatmap': [{'end': 944.281, 'start': 887.524, 'weight': 0.902}], 'summary': 'Tutorial series covers building a restful api with node.js, focusing on creating a model for orders, configuring schema and order management, handling database operations, implementing error handling, and managing product orders with an emphasis on error responses and validation logic.', 'chapters': [{'end': 151.928, 'segs': [{'end': 30.16, 'src': 'embed', 'start': 3.172, 'weight': 0, 'content': [{'end': 7.957, 'text': 'Welcome to another video in this series where we build a RESTful API with Node.js.', 'start': 3.172, 'duration': 4.785}, {'end': 14.044, 'text': 'In the previous videos, we had a look at how we work on our products and how we store products in the database.', 'start': 7.977, 'duration': 6.067}, {'end': 15.485, 'text': "And that's all working.", 'start': 14.625, 'duration': 0.86}, {'end': 17.668, 'text': 'Now I want to work on the orders.', 'start': 15.966, 'duration': 1.702}, {'end': 22.573, 'text': 'And the interesting part here, of course, is that orders are connected to products.', 'start': 18.088, 'duration': 4.485}, {'end': 24.896, 'text': "So let's dive into that in this video.", 'start': 22.833, 'duration': 2.063}, {'end': 30.16, 'text': "So let's start working on that.", 'start': 28.919, 'duration': 1.241}], 'summary': 'Building restful api with node.js, focusing on orders and products.', 'duration': 26.988, 'max_score': 3.172, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY3172.jpg'}, {'end': 107.976, 'src': 'embed', 'start': 55.45, 'weight': 1, 'content': [{'end': 57.431, 'text': 'I want to have a unique ID per order.', 'start': 55.45, 'duration': 1.981}, {'end': 62.916, 'text': 'i also need one important piece of information.', 'start': 59.093, 'duration': 3.823}, {'end': 66.619, 'text': 'i need to know which product was ordered.', 'start': 62.916, 'duration': 3.703}, {'end': 75.725, 'text': 'now. you could, of course, create a way more complex setup where you have cards, so shopping carts which comprise a couple of order,', 'start': 66.619, 'duration': 9.106}, {'end': 77.106, 'text': 'products and quantities.', 'start': 75.725, 'duration': 1.381}, {'end': 79.849, 'text': 'I want to start and stick simple here.', 'start': 77.967, 'duration': 1.882}, {'end': 85.713, 'text': 'I want to say, OK, I got an order and that order is connected to one product which I ordered.', 'start': 80.489, 'duration': 5.224}, {'end': 93.74, 'text': "So how does this work? Then I'll add a product key and I'll set this equal to an object.", 'start': 86.334, 'duration': 7.406}, {'end': 106.155, 'text': 'Now the type here will actually also be an object ID because I will store the ID of the product, which is related to this order.', 'start': 94.611, 'duration': 11.544}, {'end': 107.976, 'text': "And now there's something important.", 'start': 106.635, 'duration': 1.341}], 'summary': 'Need unique ids for orders, store product info. starting simple with one product per order.', 'duration': 52.526, 'max_score': 55.45, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY55450.jpg'}], 'start': 3.172, 'title': 'Building restful api with node.js', 'summary': 'Focuses on creating a model for orders, emphasizing the connection between orders and products, and discusses the implications of using a non-relational database for handling relations in node.js.', 'chapters': [{'end': 151.928, 'start': 3.172, 'title': 'Building restful api with node.js', 'summary': 'Focuses on creating a model for orders, emphasizing the connection between orders and products, and discusses the implications of using a non-relational database for handling relations.', 'duration': 148.756, 'highlights': ['Discussing the creation of a model for orders and its connection to products in the context of building a RESTful API with Node.js. N/A', 'Emphasizing the simplicity of connecting one order to one product and explaining the decision to stick with a simple setup. N/A', 'Highlighting the use of an object ID for storing the ID of the product related to the order, and discussing the implications of creating relations in a non-relational database like MongoDB. N/A']}], 'duration': 148.756, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY3172.jpg', 'highlights': ['Discussing the creation of a model for orders and its connection to products in the context of building a RESTful API with Node.js.', 'Highlighting the use of an object ID for storing the ID of the product related to the order, and discussing the implications of creating relations in a non-relational database like MongoDB.', 'Emphasizing the simplicity of connecting one order to one product and explaining the decision to stick with a simple setup.']}, {'end': 340.912, 'segs': [{'end': 203.63, 'src': 'embed', 'start': 152.748, 'weight': 0, 'content': [{'end': 156.27, 'text': "And as I said, first of all, let's assign the type to be an object ID.", 'start': 152.748, 'duration': 3.522}, {'end': 158.832, 'text': "And then there's an important keyword, ref.", 'start': 156.69, 'duration': 2.142}, {'end': 162.234, 'text': 'You need to use ref here to configure this type.', 'start': 159.192, 'duration': 3.042}, {'end': 169.078, 'text': 'And ref should now hold a string with the name of the model you want to connect this model to.', 'start': 163.154, 'duration': 5.924}, {'end': 173.763, 'text': 'Now, if we have a look at our product JS file, we see that the name is product.', 'start': 169.898, 'duration': 3.865}, {'end': 175.766, 'text': "So it's this name we assigned here.", 'start': 173.923, 'duration': 1.843}, {'end': 178.289, 'text': "And that's the name I now also use here.", 'start': 175.786, 'duration': 2.503}, {'end': 182.835, 'text': 'I want to connect this schema with my product.', 'start': 178.77, 'duration': 4.065}, {'end': 185.579, 'text': 'And with that, I store product.', 'start': 183.917, 'duration': 1.662}, {'end': 190.843, 'text': 'my order, where i say we ordered this product.', 'start': 186.42, 'duration': 4.423}, {'end': 195.585, 'text': "now let's say a product or excuse me, an order also knows a quantity.", 'start': 190.843, 'duration': 4.742}, {'end': 203.63, 'text': 'here we could set a type to be number and we can set required to true, because i want to have a quantity on each order.', 'start': 195.585, 'duration': 8.045}], 'summary': 'Configuring a schema to connect models, setting type and required quantity.', 'duration': 50.882, 'max_score': 152.748, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY152748.jpg'}, {'end': 254.091, 'src': 'embed', 'start': 226.219, 'weight': 2, 'content': [{'end': 228.78, 'text': "So we'll always store a quantity in the database.", 'start': 226.219, 'duration': 2.561}, {'end': 234.482, 'text': "It's just going to be our default quantity of no data or no additional information is passed.", 'start': 228.98, 'duration': 5.502}, {'end': 236.482, 'text': 'And that is a very simple order.', 'start': 235.182, 'duration': 1.3}, {'end': 238.083, 'text': 'We get the product, we get the quantity.', 'start': 236.502, 'duration': 1.581}, {'end': 246.487, 'text': 'the price is part of the product, so we can calculate the total price and with that we got a schema set up for orders.', 'start': 238.383, 'duration': 8.104}, {'end': 249.969, 'text': "now let's use that schema in our order js file.", 'start': 246.487, 'duration': 3.482}, {'end': 251.93, 'text': 'before we fetch all orders.', 'start': 249.969, 'duration': 1.961}, {'end': 254.091, 'text': "let's work on hosting them now.", 'start': 251.93, 'duration': 2.161}], 'summary': 'Default quantity stored in database for simple orders. schema set up for orders.', 'duration': 27.872, 'max_score': 226.219, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY226219.jpg'}, {'end': 312.021, 'src': 'embed', 'start': 280.916, 'weight': 1, 'content': [{'end': 284.459, 'text': 'so using our model as a constructor, which we can of course,', 'start': 280.916, 'duration': 3.543}, {'end': 298, 'text': "and then passing a javascript object where we will configure it now i'll set the id equal to mongoose types object id executed as a function to automatically generate an id.", 'start': 284.459, 'duration': 13.541}, {'end': 307.618, 'text': 'then i can set the quantity to request body quantity, though that might not be set.', 'start': 298, 'duration': 9.618}, {'end': 309.98, 'text': 'so this might be null, but it should still work.', 'start': 307.618, 'duration': 2.362}, {'end': 312.021, 'text': "we'll see if it does.", 'start': 309.98, 'duration': 2.041}], 'summary': 'Using a model constructor to configure a javascript object, setting id to mongoose types object id, and handling potential null values for quantity.', 'duration': 31.105, 'max_score': 280.916, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY280916.jpg'}], 'start': 152.748, 'title': 'Schema configuration and order management', 'summary': "Covers configuring schema and connecting models, highlighting the use of 'ref' for establishing connections and setting field properties. additionally, it explains working with order schemas, including setting default quantity, calculating total price, and creating new orders.", 'chapters': [{'end': 203.63, 'start': 152.748, 'title': 'Configuring schema and connecting models', 'summary': "Explains how to configure a schema and connect it with a model, emphasizing the use of 'ref' to establish a connection and setting the type and required properties for fields, with an example of a quantity field being set as required.", 'duration': 50.882, 'highlights': ["The 'ref' keyword is important for configuring the type and should hold a string with the name of the model being connected, as demonstrated with the 'product' model in the product JS file.", "In the schema, a field for quantity can be set as required by assigning the type to be number and setting 'required' to true, ensuring a quantity is present for each order."]}, {'end': 340.912, 'start': 203.63, 'title': 'Working with orders and schemas', 'summary': 'Explains how to create and configure an order schema, including setting default quantity, calculating total price, and creating a new order using the schema and model.', 'duration': 137.282, 'highlights': ['We always store a quantity in the database, with a default quantity of one if no additional information is passed.', 'The price is part of the product, allowing for calculation of the total price for an order.', 'Creating a new order involves configuring it using the model as a constructor and setting the id using mongoose types object id, along with setting the quantity and product based on the request body.']}], 'duration': 188.164, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY152748.jpg', 'highlights': ["The 'ref' keyword is important for configuring the type and should hold a string with the name of the model being connected, as demonstrated with the 'product' model in the product JS file.", 'Creating a new order involves configuring it using the model as a constructor and setting the id using mongoose types object id, along with setting the quantity and product based on the request body.', 'The price is part of the product, allowing for calculation of the total price for an order.', "In the schema, a field for quantity can be set as required by assigning the type to be number and setting 'required' to true, ensuring a quantity is present for each order.", 'We always store a quantity in the database, with a default quantity of one if no additional information is passed.']}, {'end': 540.709, 'segs': [{'end': 387.743, 'src': 'embed', 'start': 341.972, 'weight': 1, 'content': [{'end': 344.773, 'text': "We're storing it in the database where we're trying, we're setting it up.", 'start': 341.972, 'duration': 2.801}, {'end': 347.433, 'text': 'We can now store it by chaining save.', 'start': 345.213, 'duration': 2.22}, {'end': 348.954, 'text': 'And then.', 'start': 348.574, 'duration': 0.38}, {'end': 357.372, 'text': 'on save, we can chain then or actually we can change exec to turn it into a real promise.', 'start': 350.547, 'duration': 6.825}, {'end': 362.135, 'text': 'then and catch are also provided on the return values by default.', 'start': 357.372, 'duration': 4.763}, {'end': 363.616, 'text': "but that's not a real promise.", 'start': 362.135, 'duration': 1.481}, {'end': 380.995, 'text': "with exec it is so now we can add then and catch And then we can use the result we're getting back here to maybe log it to the console and of course to also send a status code of 201,", 'start': 363.616, 'duration': 17.379}, {'end': 384.519, 'text': 'where we essentially for now send a simple response.', 'start': 380.995, 'duration': 3.524}, {'end': 387.743, 'text': "We're going to fine-tune this where it just returned the result.", 'start': 384.599, 'duration': 3.144}], 'summary': 'Data is stored in the database using chaining save, exec converts to a real promise, allowing usage of then and catch, and returning a status code of 201 for a simple response.', 'duration': 45.771, 'max_score': 341.972, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY341972.jpg'}, {'end': 445.022, 'src': 'embed', 'start': 419.19, 'weight': 0, 'content': [{'end': 424.011, 'text': "So let's get a list of products, maybe this one with that ID, let's copy the ID.", 'start': 419.19, 'duration': 4.821}, {'end': 431.213, 'text': "Let's create a new post request, which targets slash orders to send a post request to that endpoint.", 'start': 424.692, 'duration': 6.521}, {'end': 445.022, 'text': "And I'll send that JSON body, which should be a normal JSON object, where I do set and now this is important where I do set quantity and product ID.", 'start': 431.873, 'duration': 13.149}], 'summary': 'Creating a new post request to send a json body with quantity and product id to the orders endpoint.', 'duration': 25.832, 'max_score': 419.19, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY419190.jpg'}, {'end': 540.709, 'src': 'embed', 'start': 501.925, 'weight': 4, 'content': [{'end': 504.447, 'text': "Now let's also try not passing a quantity.", 'start': 501.925, 'duration': 2.522}, {'end': 508.83, 'text': 'If I now hit send, we see we also get this default quantity of one.', 'start': 505.147, 'duration': 3.683}, {'end': 517.076, 'text': "And if I now don't send an ID, so I don't send anything, we get an error that the product is required, which makes sense.", 'start': 508.85, 'duration': 8.226}, {'end': 520.477, 'text': 'So now we can successfully create new orders.', 'start': 518.035, 'duration': 2.442}, {'end': 530.063, 'text': "Let's make sure we can also view all orders by going to our general get route here and let's use order here together with find, with no arguments,", 'start': 521.058, 'duration': 9.005}, {'end': 531.324, 'text': 'to find all orders.', 'start': 530.063, 'duration': 1.261}, {'end': 538.408, 'text': 'If you pass an argument to find it would be an object that describes the object you want to find just as we used it for update and delete.', 'start': 531.824, 'duration': 6.584}, {'end': 540.709, 'text': 'But here I want to find all orders.', 'start': 539.068, 'duration': 1.641}], 'summary': 'Successfully created new orders, view all orders using general get route.', 'duration': 38.784, 'max_score': 501.925, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY501925.jpg'}], 'start': 341.972, 'title': 'Database and order handling', 'summary': 'Covers setting up data storage in a database, implementing error handling, creating new orders, and viewing all orders.', 'chapters': [{'end': 418.73, 'start': 341.972, 'title': 'Database data storage and error handling', 'summary': 'Covers setting up data storage in a database, using chaining save and exec to turn it into a real promise, and implementing error handling with then and catch for status code 500 and 201 responses.', 'duration': 76.758, 'highlights': ['Setting up data storage in a database Covering the process of setting up data storage in a database and using chaining save.', 'Using exec to turn it into a real promise Describing the use of exec to convert the process into a real promise, enabling the addition of then and catch for handling responses.', 'Implementing error handling with then and catch Detailing the implementation of error handling using then and catch for responses with status code 500 and 201, along with logging and returning appropriate error messages.']}, {'end': 540.709, 'start': 419.19, 'title': 'Creating and viewing orders', 'summary': 'Covers creating new orders by sending a post request to the orders endpoint with a json object containing product id and quantity, then viewing all orders by using the find method with no arguments.', 'duration': 121.519, 'highlights': ['Creating a new order with quantity and product ID By sending a post request to the orders endpoint with a JSON body containing product ID and quantity, a new object with a distinct ID is created and stored, with the default quantity being one if not specified.', 'Error handling for missing product ID and quantity If a product ID is not sent, an error indicating the requirement of the product is received, ensuring proper validation. Additionally, not passing a quantity results in the default quantity of one being assigned.', 'Viewing all orders Using the find method with no arguments allows for the retrieval of all orders, providing a comprehensive view of the existing orders in the system.']}], 'duration': 198.737, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY341972.jpg', 'highlights': ['Creating a new order with quantity and product ID By sending a post request to the orders endpoint with a JSON body containing product ID and quantity, a new object with a distinct ID is created and stored, with the default quantity being one if not specified.', 'Implementing error handling with then and catch Detailing the implementation of error handling using then and catch for responses with status code 500 and 201, along with logging and returning appropriate error messages.', 'Using exec to turn it into a real promise Describing the use of exec to convert the process into a real promise, enabling the addition of then and catch for handling responses.', 'Setting up data storage in a database Covering the process of setting up data storage in a database and using chaining save.', 'Error handling for missing product ID and quantity If a product ID is not sent, an error indicating the requirement of the product is received, ensuring proper validation. Additionally, not passing a quantity results in the default quantity of one being assigned.', 'Viewing all orders Using the find method with no arguments allows for the retrieval of all orders, providing a comprehensive view of the existing orders in the system.']}, {'end': 739.642, 'segs': [{'end': 629.518, 'src': 'embed', 'start': 541.93, 'weight': 0, 'content': [{'end': 545.853, 'text': 'And I simply will execute exec to again turn it into a real promise here.', 'start': 541.93, 'duration': 3.923}, {'end': 548.294, 'text': 'And then chain then and catch.', 'start': 546.453, 'duration': 1.841}, {'end': 550.476, 'text': "And then let's see what we can do here.", 'start': 549.155, 'duration': 1.321}, {'end': 552.517, 'text': "Let's use all the docs we get back.", 'start': 550.736, 'duration': 1.781}, {'end': 556.82, 'text': 'And for now simply return them with a status code of 200.', 'start': 553.198, 'duration': 3.622}, {'end': 558.822, 'text': 'JSON docs like that.', 'start': 556.82, 'duration': 2.002}, {'end': 571.871, 'text': 'and for an error i will send a status code of 500 where i set json to an object where we store and return the error.', 'start': 559.722, 'duration': 12.149}, {'end': 579.876, 'text': "now, with that set up, let's save this and let's try it out by sending a get request to orders like this.", 'start': 571.871, 'duration': 8.005}, {'end': 584.921, 'text': 'If I hit send here, I get all the orders I did create.', 'start': 580.717, 'duration': 4.204}, {'end': 590.667, 'text': 'So the two with quantity two I stored and the one with quantity one.', 'start': 585.462, 'duration': 5.205}, {'end': 595.653, 'text': 'so this is now my get request.', 'start': 592.071, 'duration': 3.582}, {'end': 599.114, 'text': "now let's already work on the response layout.", 'start': 595.653, 'duration': 3.461}, {'end': 607.038, 'text': "so here, for example, i don't want to return that v part and i want to return more information than just the array of items.", 'start': 599.114, 'duration': 7.924}, {'end': 616.502, 'text': "so i'll add select prior to exec and just select the product and the quantity field and the id, let's say,", 'start': 607.038, 'duration': 9.464}, {'end': 629.518, 'text': "and then again here I'll return an object where I have count, which is Doc's length, and where I then also have my orders, which is now Doc's,", 'start': 617.608, 'duration': 11.91}], 'summary': 'Implemented a function to handle get request for orders, returning json data with status codes 200 and 500 for success and error, respectively.', 'duration': 87.588, 'max_score': 541.93, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY541930.jpg'}, {'end': 739.642, 'src': 'embed', 'start': 660.16, 'weight': 1, 'content': [{'end': 668.647, 'text': 'I should map it here to get the individual doc and then return a new object here for that document we got.', 'start': 660.16, 'duration': 8.487}, {'end': 671.51, 'text': 'where I then have my ID, that is doc.', 'start': 668.647, 'duration': 2.863}, {'end': 675.113, 'text': 'underscore ID where I have my product, that is doc.', 'start': 671.51, 'duration': 3.603}, {'end': 678.032, 'text': 'product id.', 'start': 676.51, 'duration': 1.522}, {'end': 679.835, 'text': 'so just excuse me, just product.', 'start': 678.032, 'duration': 1.803}, {'end': 680.676, 'text': 'what we got here?', 'start': 679.835, 'duration': 0.841}, {'end': 684.061, 'text': 'so just product, and we then got the quantity,', 'start': 680.676, 'duration': 3.385}, {'end': 693.827, 'text': 'which is stock quantity and where we then pass our request object and where we now can use doc underscore id Like this', 'start': 684.061, 'duration': 9.766}, {'end': 698.07, 'text': 'Now, if we save this, we get the same logic as in our product routes.', 'start': 694.728, 'duration': 3.342}, {'end': 701.653, 'text': 'I get this more structured response with more information.', 'start': 698.671, 'duration': 2.982}, {'end': 706.597, 'text': 'The same makes sense for creating a new object here.', 'start': 702.554, 'duration': 4.043}, {'end': 713.862, 'text': 'If we succeeded, I might want to return a message where I say order stored.', 'start': 706.897, 'duration': 6.965}, {'end': 719.895, 'text': 'and where I then pass a URL where you can get more information like the type.', 'start': 715.293, 'duration': 4.602}, {'end': 727.637, 'text': 'Here you could then set the type to get and the URL would essentially be the same as we use up here.', 'start': 720.555, 'duration': 7.082}, {'end': 737.561, 'text': 'So I can just copy that URL down to this return response here, access it on the result I get.', 'start': 728.038, 'duration': 9.523}, {'end': 739.642, 'text': 'This is the object that was created.', 'start': 737.901, 'duration': 1.741}], 'summary': 'Mapping and returning a new object with id and product id, and displaying a structured response with more information.', 'duration': 79.482, 'max_score': 660.16, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY660160.jpg'}], 'start': 541.93, 'title': 'Handling get requests and response layout, and mapping and creating new object', 'summary': 'Covers executing get requests, response layout handling, and error handling. it also discusses mapping, creating new objects, using ids and passing request objects, and returning messages upon successful object creation with a url for additional information.', 'chapters': [{'end': 659.5, 'start': 541.93, 'title': 'Handling get requests and response layout', 'summary': 'Covers the execution of get requests, handling of response layout, and error handling. it demonstrates sending a get request to retrieve stored orders, modifying the response layout, and setting up error handling with specific status codes.', 'duration': 117.57, 'highlights': ['The chapter demonstrates handling get requests by sending a get request to retrieve stored orders.', 'The speaker modifies the response layout by adding a select prior to exec to return specific fields like product, quantity, and id, and then constructs an object with count, orders, and request object.', 'Error handling is set up with specific status codes; a status code of 500 is sent for errors, and the JSON object stores and returns the error.']}, {'end': 739.642, 'start': 660.16, 'title': 'Mapping and creating new object', 'summary': 'Discusses mapping to retrieve individual documents and creating new objects, including the use of ids, product information, and passing request objects to achieve a more structured response, as well as returning a message upon successful creation of an object along with a url for additional information.', 'duration': 79.482, 'highlights': ['Mapping to retrieve individual documents and creating new objects with structured response', 'Using IDs and product information in document retrieval and creation', 'Returning a message upon successful creation of an object along with a URL for additional information', 'Passing request objects for document creation']}], 'duration': 197.712, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY541930.jpg', 'highlights': ['The chapter demonstrates handling get requests by sending a get request to retrieve stored orders.', 'Mapping to retrieve individual documents and creating new objects with structured response', 'The speaker modifies the response layout by adding a select prior to exec to return specific fields like product, quantity, and id, and then constructs an object with count, orders, and request object.', 'Using IDs and product information in document retrieval and creation', 'Error handling is set up with specific status codes; a status code of 500 is sent for errors, and the JSON object stores and returns the error.', 'Returning a message upon successful creation of an object along with a URL for additional information', 'Passing request objects for document creation']}, {'end': 1321.031, 'segs': [{'end': 861.329, 'src': 'embed', 'start': 802.43, 'weight': 0, 'content': [{'end': 805.792, 'text': "So that is something we'll have to make sure that it doesn't happen.", 'start': 802.43, 'duration': 3.362}, {'end': 811.556, 'text': "So let's turn or let's add some logic to control this.", 'start': 806.933, 'duration': 4.623}, {'end': 816.599, 'text': "We want to make sure that we can't create new orders for products we don't have.", 'start': 812.296, 'duration': 4.303}, {'end': 827.066, 'text': "So here in our post method in the orders.js file, I'll first of all check if we actually do have a product for a given ID.", 'start': 818.38, 'duration': 8.686}, {'end': 837.013, 'text': "Now to do that, I'll import product here by calling require and going to the models folder and then the product file.", 'start': 827.826, 'duration': 9.187}, {'end': 841.776, 'text': 'And then I can use a static method on product here.', 'start': 838.153, 'duration': 3.623}, {'end': 849.112, 'text': 'call find by ID and search for request body product ID.', 'start': 843.325, 'duration': 5.787}, {'end': 858.247, 'text': 'And now what I can do is if this succeeds Then I want to continue with creating the order.', 'start': 850.894, 'duration': 7.353}, {'end': 861.329, 'text': 'Otherwise, I want to return an error.', 'start': 859.127, 'duration': 2.202}], 'summary': 'Implement logic to prevent creating orders for unavailable products in the post method of orders.js.', 'duration': 58.899, 'max_score': 802.43, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY802430.jpg'}, {'end': 944.281, 'src': 'heatmap', 'start': 887.524, 'weight': 0.902, 'content': [{'end': 888.365, 'text': 'Well, it is a product.', 'start': 887.524, 'duration': 0.841}, {'end': 889.627, 'text': "Let's maybe name it like this.", 'start': 888.425, 'duration': 1.202}, {'end': 897.757, 'text': 'And then in here, I can execute the other code here to store that new product.', 'start': 890.228, 'duration': 7.529}, {'end': 900.48, 'text': 'to store that new order.', 'start': 898.82, 'duration': 1.66}, {'end': 904.861, 'text': "however, now we have a nice level of nesting which we don't want when using promises.", 'start': 900.48, 'duration': 4.381}, {'end': 916.424, 'text': 'so what we can do is i can simply return order, save which does return a promise, as you see, and then i can use that,', 'start': 904.861, 'duration': 11.563}, {'end': 922.345, 'text': 'the then and the catch blocks and chain them after.', 'start': 916.424, 'duration': 5.921}, {'end': 931.453, 'text': "here just means that this catch block here isn't too useful anymore because it'll never execute because we have this other catch block here.", 'start': 922.345, 'duration': 9.108}, {'end': 938.678, 'text': 'So now with that we got a setup where I check if we do have a product before, I then try to save it,', 'start': 932.253, 'duration': 6.425}, {'end': 944.281, 'text': 'and we then execute all the other steps for creating the order and for returning a response.', 'start': 938.678, 'duration': 5.603}], 'summary': 'Code refactored to handle promise chaining for product order creation', 'duration': 56.757, 'max_score': 887.524, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY887524.jpg'}, {'end': 1321.031, 'src': 'embed', 'start': 1313.202, 'weight': 2, 'content': [{'end': 1321.031, 'text': "now working with all that data and seeing how we can further evolve our RESTful API is what we'll dive into next.", 'start': 1313.202, 'duration': 7.829}], 'summary': 'Evolving restful api with data analysis.', 'duration': 7.829, 'max_score': 1313.202, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY1313202.jpg'}], 'start': 740.322, 'title': 'Managing product orders', 'summary': 'Covers creating, retrieving, and deleting product orders, including error handling and validation, and emphasizes the importance of error responses and avoiding redundant data. it discusses implementing validation logic to prevent creating orders for non-existing products.', 'chapters': [{'end': 827.066, 'start': 740.322, 'title': 'Order creation and validation logic', 'summary': 'Discusses creating an order object, outputting order information, and implementing validation logic to prevent creating orders for non-existing products in the database.', 'duration': 86.744, 'highlights': ['The chapter discusses creating an order object and outputting the id, product, and quantity from the result.', 'It mentions reusing a product id and storing the response, along with handling invalid product ids.', 'The chapter emphasizes the need to add logic to prevent the creation of orders for products that do not exist in the database.']}, {'end': 1321.031, 'start': 827.826, 'title': 'Managing product orders', 'summary': 'Covers creating, retrieving, and deleting product orders, including error handling and validation, such as checking for existing products and orders, with emphasis on the importance of error responses and avoiding redundant data.', 'duration': 493.205, 'highlights': ['The chapter covers creating, retrieving, and deleting product orders, including error handling and validation. Covers the main operations related to product orders.', 'Emphasizes the importance of error responses and avoiding redundant data. Highlights the significance of error handling and data validation.', 'Discusses checking for existing products and orders, with emphasis on the importance of error responses and avoiding redundant data. Focuses on the process of verifying the existence of products and orders.']}], 'duration': 580.709, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/VKuY8QscZwY/pics/VKuY8QscZwY740322.jpg', 'highlights': ['The chapter covers creating, retrieving, and deleting product orders, including error handling and validation. Covers the main operations related to product orders.', 'The chapter emphasizes the need to add logic to prevent the creation of orders for products that do not exist in the database.', 'Emphasizes the importance of error responses and avoiding redundant data. Highlights the significance of error handling and data validation.']}], 'highlights': ['Creating a model for orders and its connection to products in the context of building a RESTful API with Node.js.', "The 'ref' keyword is important for configuring the type and should hold a string with the name of the model being connected, as demonstrated with the 'product' model in the product JS file.", 'Creating a new order involves configuring it using the model as a constructor and setting the id using mongoose types object id, along with setting the quantity and product based on the request body.', "In the schema, a field for quantity can be set as required by assigning the type to be number and setting 'required' to true, ensuring a quantity is present for each order.", 'Creating a new order with quantity and product ID By sending a post request to the orders endpoint with a JSON body containing product ID and quantity, a new object with a distinct ID is created and stored, with the default quantity being one if not specified.', 'Implementing error handling with then and catch Detailing the implementation of error handling using then and catch for responses with status code 500 and 201, along with logging and returning appropriate error messages.', 'Using exec to turn it into a real promise Describing the use of exec to convert the process into a real promise, enabling the addition of then and catch for handling responses.', 'Setting up data storage in a database Covering the process of setting up data storage in a database and using chaining save.', 'Error handling for missing product ID and quantity If a product ID is not sent, an error indicating the requirement of the product is received, ensuring proper validation. Additionally, not passing a quantity results in the default quantity of one being assigned.', 'Viewing all orders Using the find method with no arguments allows for the retrieval of all orders, providing a comprehensive view of the existing orders in the system.', 'The chapter demonstrates handling get requests by sending a get request to retrieve stored orders.', 'Mapping to retrieve individual documents and creating new objects with structured response', 'The speaker modifies the response layout by adding a select prior to exec to return specific fields like product, quantity, and id, and then constructs an object with count, orders, and request object.', 'Using IDs and product information in document retrieval and creation', 'Error handling is set up with specific status codes; a status code of 500 is sent for errors, and the JSON object stores and returns the error.', 'Returning a message upon successful creation of an object along with a URL for additional information', 'The chapter covers creating, retrieving, and deleting product orders, including error handling and validation. Covers the main operations related to product orders.', 'The chapter emphasizes the need to add logic to prevent the creation of orders for products that do not exist in the database.', 'Emphasizes the importance of error responses and avoiding redundant data. Highlights the significance of error handling and data validation.']}