Genius level series. I love how you show default return data from the API and then how you make it more relevant to the function that the particular method is performing. It is also very enlightening when there is a particular error, such as the exec() on the post for orders, that you explain why that happened. Glad this series popped up on my radar. I appreciate your hard work and teaching!
@academind5 жыл бұрын
Thank you Haakon, feedback like yours definitely keeps me motivated :)
@ALEXEIS6 жыл бұрын
I really like the way you teach. You always explain why "what is what". Your smiles are beautiful as well. But they can also be annoying when I'm not getting something right :)
@academind6 жыл бұрын
Happy to read that you like my videos! I definitely try to keep that happy attitude, because although coding can be tough, we should enjoy what we do whenever possible :)
@benjilightstone58725 жыл бұрын
Your videos are amazing, as I'm sure you know! I love how to take the time to explain everything, even if it may be very basic concepts because it helps us learn how much better. I love how you branched the git for this tutorial series, it makes it so much easier to follow along and see where I went wrong. Thanks again Max!
@academind5 жыл бұрын
Thank you very very much for your wonderful feedback Benji, just great to read that you enjoy the series :)
@phatienza6 жыл бұрын
calm, composed and very patient. Best teacher!
@academind6 жыл бұрын
Thank you very much for your awesome feedback, it really means a lot to me to read that!
@free2idol12 жыл бұрын
16:49: I think there is a bug here. I've just tested and seen that even if "return" is used, the code WOULD NOT stop executing there, which means the 2nd then() block will still be run no matter what the result of the "Product.findById" is. It's because there is no exception triggered in the first then() block (just the doc is not found) so the code will keep going down to the next then() instead of exiting. So when the 2nd then() block is run, the response would be sent again by this code "res.status(201).json(...)" and as a result "cannot set headers after they are sent to the client" exception will be thrown.
@RobertPodosek2 жыл бұрын
@Aditya Jaiman same. time to quit the series I guess
@sthembisovinjwa869311 ай бұрын
Use res.statusCode to check if the status code was already set before setting it again in the new then/catch block. If it was set, then just return;
@apigamafio6 ай бұрын
try this code, with an if statement to validate for the product's existence, while also returning an 404 error for Product not found. The return statement inside the first .then statement will prevent the second .then statement from running. code below: Product.findById(req.body.productId) .then(product => { if (!product) { return res.status(404).json({ message: "Product not found in Database...", error: err }) } const order = new Order({ _id: new mongoose.Types.ObjectId(), quantity: req.body.quantity, product: req.body.productId }) return order.save() }) .then(result => { console.log(result) res.status(201).json({ message: "Order created successfully!", createdOrder: { _id: result._id, product: result.product, quantity: result.quantity }, request: { type: 'GET', description: "Get Info on the Newly Created Order!", url: '/orders/' + result._id } }) }) .catch(err => { res.status(500).json({ message: "Order could not be stored correctly...", error: err }) })
@WilcoGroothand2 жыл бұрын
Max, still very spot on. Like the way you guys work at academind. Very good way to start this old dog learning new things after years of not programming at all. Clear tutorial. Thx.
@anuragtiwari66596 жыл бұрын
If this could have just came 2 months back....you could have saved me a infinite amount of hard work I needed to do to find all these and still can't find with this crystal clear explanation. God is not in heaven.. He is on Earth and I can see that.. Today is 31 dec and I find it so interesting that on the occasion of new year all I am going to do is watch these tutorials because this is too much fun.
@academind6 жыл бұрын
Awesome to hear that, thank you so much!
@TheNerdyDev6 жыл бұрын
Thanks Max ...Your way of explaining the things is quite crystal clear...And by the way this series of yours is 100 times better than some Moocs I took months ago...because you make it so easy to grasp even slightest of the things and we come to know about the latest coding conventions from you.( I use to work with async await but even promises is quite good to work with)
@academind6 жыл бұрын
It really means a lot to me to read that Prateek, thank you very much! So happy to see that my way of explaining helped to make things clearer for you :)
@satishkumar-qq8df2 жыл бұрын
Your videos are really nice, this is how we learners expect to be explained.
@kevinnguyen18816 жыл бұрын
Hey Max just wanna say thank you so much for this free content! Its been making things crystal clear.
@academind6 жыл бұрын
Thanks for your awesome feedback - really great to hear this! :)
@Sun1ive6 жыл бұрын
Thank you Max for your videos. Atm going through your Vue course. If its possible in future would like to see a Vue+(express maybe?)+mongo or sql project. Cheers
@faisaljutt78416 жыл бұрын
Thank a ton Max, i always follow your series of node.js and other tutorials as well. You really make it a piece of cake :) Please make a video on "how to scale node.js application" to make it faster and lightweight. Thanks!
@academind6 жыл бұрын
Thanks for your very nice feedback Faisal - and of course for the suggestion. I'll see what I can do ;)
@kumareth5 жыл бұрын
You are so nice in teaching, and also "serious" in the series. 😅
@academind5 жыл бұрын
Haha, thanks a lot!
@Raptor-wz8zi6 жыл бұрын
Thank you very much for this course. You are by far the best teacher I had and trust me I had many before.
@academind6 жыл бұрын
Thank you so much for your awesome feedback Charl, this really means a lot to me :)
@DuyTran-ss4lu4 жыл бұрын
This guy is a legend.
@hotwings93827 ай бұрын
For the post request to work, you need to set new in _id: new mongoose.Types.ObjectId() and also delete exec()
@knightonhd11445 жыл бұрын
Excellent tutorial, but the fast copying and pasting made it a bit fuzzy. it took me a few rewinds to catch up. Thank goodness for the source code!
@GruniTheThird6 жыл бұрын
How would I add multiple products to ONE order?? make it an array and push product IDs?
@bhumanyukumar66745 жыл бұрын
@Narasimha Kamath by creating another model named 'OrderItem" having fields like 'productid', 'quantity'.
@aiho3225 жыл бұрын
same question here!
@Pedrothegoldenspatan5 жыл бұрын
Have we gotten an answer to this question??
@lucidmorto4 жыл бұрын
you can use insertMany method
@elecktromaestimmy4 жыл бұрын
As far as I know: In your model use[{ref to products }] => indicates that you are expecting one or more products as reference. In your route: Check if input (req.body) is an array, if not convert it to one by using New Array(req.body). Then just add that to your model order = new product({ ... products = Array(req.body) })
@jasonqu9746 Жыл бұрын
for any future watchers, if you're getting the error "product: ValidatorError: Path `product` is required." at around 8:00 when attempting to create the order, make sure you're setting the content-type in postman to JSON.
@el_polako_885 жыл бұрын
For everyone facing UnhandledPromiseRejectionWarning while doing POST request with incorrect productId, maybe this will help: in second "then" , try to add this code at the beginning: if(res.statusCode===404){ return res; } Now warning disappear and we have a right error code. I hope it helped?
@sriramtorvi74175 жыл бұрын
Thanks buddy this helped..
@Wakkyguy4 жыл бұрын
Why does it reach this point even after returning? Thanks anyway.
@mekonnenhaileslassie56514 жыл бұрын
thanks bro!
@hansand_4 жыл бұрын
hi karol, it works,thanks . can you please explain why its not reach the catch ?
@mike304763 жыл бұрын
Dude!!! I'm stuck on this for almost 1 hour!!! I love You!!!
@cbrudder846 жыл бұрын
I would suggest using the .serialize() method to avoid having to retype all of those response objects.
@zainuddin12053 жыл бұрын
Thank you for such wonderful contents for free!
@novanuke13564 жыл бұрын
How would you create an order model that could have multiple products? An array of product IDs?
@SPARTAN87467 ай бұрын
Yes, but I would recommend an array of product objects. NoSQL DBs are supposed to be used like that: To store objects, not link everything with IDs. If you want to use ID to link everything, it is better to use a SQL DB.
@pei-yaochang32346 жыл бұрын
You're the best teacher!! Thanks.
@academind6 жыл бұрын
Thank you so much, this really means a lot to me :)
@praveenpawar90846 жыл бұрын
nice tutorial, i liked the way of teaching
@academind6 жыл бұрын
That's really great to read, thank you for your comment!
@anythingbutcoding92113 жыл бұрын
Great lectures buddy 👍🏻
@snaplu46833 жыл бұрын
I think use async/await along with try/catch would be better in configuring route
@jojojawjaw5 жыл бұрын
Most helpful tutorial, maybe ever. Thank you really damn much!
@academind5 жыл бұрын
Just awesome to read that Sarah, thank YOU for this fantastic feedback!
@MissaMelodica5 жыл бұрын
19:13 nice segue mate!
@academind5 жыл бұрын
Thank you :)
@bomaspeedy23028 ай бұрын
Hi there, I get an error when trying to POST an order Here is the error { “error”: { “ message “ : “ Class constructor ObjectId cannot be invoked without ‘ new ‘ “ } }
@nathanieljames86777 ай бұрын
I also got the error and just added 'new' in the _id: new mongoose.Types.ObjectId(), const order = new Order({ _id: new mongoose.Types.ObjectId(), product: req.body.productId, quantity: req.body.quantity });
@spectrecular97216 жыл бұрын
6:06 - Anybody know what keyboard shortcut automatically formats the code in that fashion?
@academind6 жыл бұрын
Just look for "Format Document" in the Keyboard Shortcuts of VS Code :)
@justingomez65545 жыл бұрын
look up an npm package called prettier. Thats a tools that will formats your code for you. its very minor set up too. www.npmjs.com/package/prettier
@PandaTheGFX3 жыл бұрын
Awesome video!
@vovk18056 жыл бұрын
Tell me please, how to beautify promise chain after save? (what setting is set?)
@NawafXRMFC6 ай бұрын
in 2024 but its still awesome waiting for Express , mongoDB
@galwayiilt4 жыл бұрын
It might be a bug in my code, but I was getting the "Cast to ObjectId failed" error in the POST router in "orders.js" whenever I entered an incorrect productId. I worked around the issue by wrapping the Product.findById().then().then().catch() code in an if-else statement: if(mongoose.Types.ObjectId.isValid(req.body.productId)) { Max's code here (no need to check for !product) } else { console.log("invalid product id"); return res.status(500).json({ message: "invalid product id" }); }
@mattdaroo27904 жыл бұрын
Thanks for the solution. I had exactly same problem.
@geTTh1s2 жыл бұрын
what does the return in your else case do? we get the same result without a return before "res.status...." dont we?
@galwayiilt2 жыл бұрын
@@geTTh1s Yes, you are correct. It's been a while since I worked on this project but I suspect I added the 500 response so I could handle the error gracefully on the client end instead of having the request just fail without letting the user know the reason...
@geTTh1s2 жыл бұрын
@@galwayiilt i mean the key word "return" specifically though. We get the same result when we leave that little word out right, only writing res.status(500)...
@DieterWoestemeier6 жыл бұрын
Shouldn't you add a findById check when deleting an order? If you send a DELETE request for an invalid orderID you get back a response with "Order deleted", but that ID never existed.
@venkateswarank11455 жыл бұрын
Can someone please elaborate the use of exec() Where to use exec() and where not. I am quite confused with that. Thanks in advance :)
@jritzeku3 жыл бұрын
Perhaps this may help from what I understand. 3 differnt cases below without all the excess code for simplicity. //case1: router.get("/", (req, res, next) => { Order.find() .select("product quantity _id") //.exec() //***Without exec( ) would not be able to write a catch block below .then(docs => { }); }) // .catch(err => { //***CANT use catch block here since no exec( ) // res.status(500).json({ // error: err // }); // }); }); //case2: router.get("/", (req, res, next) => { Order.find() .select("product quantity _id") .exec() //***with exec( ) , we now have a REAL promise so can use thecatch block as follows .then(docs => { }); }) .catch(err => { //***CANT use catch block here since no exec( ) res.status(500).json({ error: err }); }); }); //case3: router.post("/", (req, res, next) => { Product.findById(req.body.productId) return order.save(); }) //***Here we do not need exec because with save( ) (executed above)you get a REAL promise by default so no need for exec( ) .then(result => { }) .catch(err => { }); });
@jritzeku3 жыл бұрын
However, I am able to execute the code inside the catch blocks where we catch any 500 status errors without exec( ). So now I don't see any use of exec( ) at least based on how Max justified its usage.
@kiizaofficial Жыл бұрын
thank you teacher for share education for everyone
@feelingeverfine6 жыл бұрын
Hey, really enjoying your videos but one thing is bugging me. Why do you explicitly set the Mongo ID? It automatically creates one for you. Why do the work?
@academind6 жыл бұрын
Actually only to highlight that this id is an important part (as we then use it to fetch elements by it). Should've made that more clearer though, you are right
@victormultiverse57996 жыл бұрын
You're the best man
@academind6 жыл бұрын
YOU are the best Victor, thank you for your support!
@Pedrothegoldenspatan5 жыл бұрын
How do I alter the post to grab/hold multiple products? @Academind
@abdelrahman23486 жыл бұрын
I am lost after 15:00
@victormultiverse57996 жыл бұрын
x2
@alexandertarasenko30386 жыл бұрын
I reviewed this reference model three or four times, and only now begin to understand it) I think that's okay. Also, I think it's difficult, because mongodb uses another model called "Embedded documents" more often, which in my opinion is easier
@TheSergy125 жыл бұрын
I need serious help here. If i add the : if(!product){ return res.status... Valid products id return an error too :(
@zealousbroadcast61974 жыл бұрын
i know i am 1 year late to reply, but answer is first see his callback,promises and async await video then you will not get lost from 15:00
@AbhishekKumar-mq1tt6 жыл бұрын
Thank u for this awesome video
@academind6 жыл бұрын
Thanks again for your great support Abhishek :)
@jes62394 жыл бұрын
Is there a form to also display the name of the product and the price on the body?
@TheProfessorsClass4 жыл бұрын
what is the Builder utility you are using @7:30
@jasonvaesen4 жыл бұрын
i keep getting this error at 8:00, and i havent a clue how to solve it, can anyone help Error: Order validation failed: product: Path `product` is required.
@saptarshiadhikary95424 жыл бұрын
Error: Order validation failed: product: Path `product` is required.
@saptarshiadhikary95424 жыл бұрын
User is already required, so the following throws an error user: { type: mongoose.Schema.ObjectId, ref: 'User', required: true } Change to user: { type: mongoose.Schema.ObjectId, ref: 'User' }
@ruslankrivoshein28935 жыл бұрын
Why does he write 'POST' in 'DELETE' method??
@alexandergutierrez1144 жыл бұрын
If I wanted to update de price everytime I POST an order. How would I be able to do that?
@SirGNPBraz6 жыл бұрын
How you change your code verticaly?
@shayantanmitra85432 жыл бұрын
I want to ask that in order schema if i want to add the user detail(like the users id,name,address,phone,email) what i need to do?
@vaibhavprakash41595 жыл бұрын
if we want to link any other field like name or roll_no then what do we edit in the code?
@CityCoder4 жыл бұрын
how sir changed all occurence at.048
@javascript_developer6 жыл бұрын
At order.js router.get() why did you create .map() method ? We can get that _id by result._id also. You used .map on get and not in post. Would you care to explain this please.
@jjferreirapt3 жыл бұрын
Can you make a video using MySQL instead of MongoDB?
@victorjozwicki81795 жыл бұрын
14:15 Forgot an exec() ?
@felixsamuel27695 жыл бұрын
Hello Academind, thank you. My get request for the orders keeps return resolving to error which I find weird as I have your exact code
@MuhammadumairAkram02763 жыл бұрын
same here
@MuhammadumairAkram02763 жыл бұрын
if u sent a raw request from postman, change type from text to json. or u can post request using form-urlencoded method of postman
@varunsingh82356 жыл бұрын
Hey hi!! Can u help me by providing the tutorial that how can u relate more then 2 table..
@travelwith9995 жыл бұрын
try to add one product twice and observe that just quantity of that should be increased and not add the duplicate entry in the database.
@travelwith9995 жыл бұрын
i don't think it will work...you need to validate orderlist and check every time that product you are adding is exit in your orderlist or not.@Narasimha Kamath
@ПетроБойко-ц3б6 жыл бұрын
Hello. In orders.js when I try to save order with productId that does not exist I get error: UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Can't set headers after they are sent.
@ПетроБойко-ц3б6 жыл бұрын
I figured out how to make it work correctly. In router.post() insted of if (!product) { return res.status(404).json({ message: "Product not found" }); } I wrote : if (!product) { throw "Product not found"; }
@_nowayout6 жыл бұрын
I get the same error and I think there is something wrong with chaining promise like that. When (!product) is true (productId does not exist) we are returning res.status(404).json(..) to the next then() block where there is another response: res.status(201).json(..). Your solution with changing return to throw is indeed working but it's not giving the same result. We are getting status 500 from the last catch block instead of just 404 from the first if statement.
I ran into this too, and rewrote the route handlers using async/await. Allows you to return the response as originally intended. router.post('/', async (req, res, next) => { try { let product = await Product.findById(req.body.productId).exec(); if (!product) { return res.status(404).json({ message: "Product not found" }); } const order = new Order({ _id: mongoose.Types.ObjectId(), quantity: req.body.quantity, product: req.body.productId }); let result = await order.save(); res.status(201).json({ message: 'Order saved', createdOrder: { _id: result._id, product: result.product, quantity: result.quantity }, request: { type: 'GET', url: 'localhost:5000/orders/' + result._id } }); } catch (err) { res.status(500).json({error: err.message}); } });
@anandlokhande10796 жыл бұрын
Correct! Faced the same problem. What I did is added a check in the secnd .then() to first check if the result.statusCode !== 404. If it's not 404 then only proceed to return response 201 along with created order data.
@vishvamurthy80895 жыл бұрын
Can I use .findOneById inside a .then promise ?
@AlexanderPalm_756 жыл бұрын
Hast du auch den Fall betrachtet wenn ein Produkt gelöscht wird was bereits in einer Bestellung verwendet wurde?
@academind6 жыл бұрын
Nein - generell behandelt diese API nicht alle möglichen Kombinationen sondern lieber möglichst viele unterschiedliche Aspekte (bspw. verschiede HTTP Verben, Auth (kommt noch) etc).
@kswetha11766 жыл бұрын
required: true is not working for me, it is accepting the orders with no properties being provided in the req body. Please help !!
@arivincenti6 жыл бұрын
Hi!! How would i search for an order by the _id of a product?
@shashaanklearns4 жыл бұрын
The orders.js generates new orders everytime you add the same /:productid and send a post request to the /orders even though the same product is already in the orders list, instead how do we manipulate that, to just add the quantity of that particular product everytime the same product is posted to the /orders
@aghatage64434 жыл бұрын
How to consume these api from node js ejs pages
@80sunblade5 жыл бұрын
At 16:55 an UnhandledPromiseRejectionWarning is generated. >> Cannot set headers after they are sent to the client
@barbiegirlincali4 жыл бұрын
Could somebody explain why we are using exec()???
@oussamakhalfi17514 жыл бұрын
In Mongoose queries are composed but not necessarily run immediately. They return a query object that you can add (or chain) other queries to. The queries are all run as a unit when needed so there isn't any storing of unneeded intermediate results. This is what the exec() function does. You'll sometime hear it referred to as "lazy loading" or "lazy evaluation" teamtreehouse.com/community/what-exactly-does-the-mongoose-exec-method-do
@barbiegirlincali4 жыл бұрын
@@oussamakhalfi1751 Thank you!
@marcin2x44 жыл бұрын
When entering invalid productId that doesn't exist in products, has anyone observed that some ids return `Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters`?
@s_k125 жыл бұрын
I don't understand why at 15:09 we write return order.save(). Why do we need 'return' keyword? What happens when we return a promise? Do we still hit exception handling if it goes wrong?
@geTTh1s2 жыл бұрын
Hey you found the answer to your question? I'm wondering the same
@s_k122 жыл бұрын
@@geTTh1s It's been a while. 😀 I think the "return" simplifies the creation of a promise in the scope of innermost curly brackets, other promises in that chain execute as before.
@geTTh1s2 жыл бұрын
@@s_k12 not sure if I get what you mean but thanks anyway haha. The return doesn't change the outcome though as I tried it with and without
@saptarshiadhikary95424 жыл бұрын
Error: Order validation failed: product: Path `product` is required.
@RakeshBitling4 жыл бұрын
create an REST API using Node.js having tweeter like functionalities: ● Authentication / Login ● Create / Read / Delete tweets ● Reply to a tweet == I need help in creating schema
@muhanadtur87856 жыл бұрын
i keep getting this error when hitting the "/orders/" "POST" endpoint, when placing a new order with invalid product id: UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after theyare sent to the client. did anyone face the same error?! i get it why i'm getting this error but how could i resolve it ?!
@muhanadtur87856 жыл бұрын
i could manage to workaround the error by refactoring the "/orders/" "POST" endpoint like this: router.post("/", (req, res, next) => { Product.findById(req.body.productId) .then(product => { if (!product) { return res.status(404).json({ message: "Product not found" }); } const order = new Order({ _id: mongoose.Types.ObjectId(), quantity: req.body.quantity, product: req.body.productId, }); order.save().then(doc => {
@muhanadtur87856 жыл бұрын
i gave it another try and did this: router.delete('/delete/:orderId', (req, res, next) => { const id = req.params.orderId; Order.findById(id) .exec() .then(order => { if (!order) { return res.status(200).json({
@muhanadtur87856 жыл бұрын
another approach : router.delete('/delete/:orderId', (req, res, next) => { const id = req.params.orderId; Order.findById(id) .exec() .then(order => { if (!order) { res.status(200).json({ message: 'order does not exist' }); return;
@pbdivyesh6 жыл бұрын
Hello Max, love your videos, but in this tutorial I got stuck after implementing the router.post('/', ....), whenever I try to POST the order in postman software it comes out as { "error": { "message": "document must have an _id before saving", "name": "MongooseError" } } and nothing gets posted in localhost:3000/orders I mean why does the mongoose expect the `_id` to be there first, isn't the productId should suffice for that? I"m sorry if I'm vague , provide any explanation or suggestion that will help me guide
@sunilmondem65876 жыл бұрын
router.post('/products',function(req,res){ var product = new Products({ _id: mongoose.Types.ObjectId(), name:req.body.name, price:req.body.price }); product.save().then(function(data){ res.send(data); }) });
@arunkumartopagi88716 жыл бұрын
Hye What is keyboard shortcut for aligning order.save().exec().then().catch();
@giguerea6 жыл бұрын
does not work for me, I would like to know this trick
@rasmushansen23916 жыл бұрын
vs extension called Prettier github.com/prettier/prettier-vscode
@Skaxarrat6 жыл бұрын
Alt + Shift + F
@mindaugasjuska6 жыл бұрын
Hi, Max. It is very hard to follow your fast scrolling at 15-16 minutes. Had to re-watch and re-play several times till I get it. Just a reminder to slow it down other time. Otherwise, great content!
@academind6 жыл бұрын
Thank you for the hint Mindaugas, I'll try my best to keep that in mind :)
@fangcao23773 жыл бұрын
Is this a bug? in the POST route, there are 2 then promise after Product.findById(req.body.product_id): the first promise returns "Product not found" and the second promise is executed too. check this out: // ===== Test ===== const promise1 = new Promise((resolve, reject) => { resolve('Success!'); }); promise1.then((value) => { console.log("==== then 1 ====") return 0 }) .then(value => { console.log("==== then 2 ====") }); // _____ Test _____ // ====== output ===== ==== then 1 ==== ==== then 2 ====
@Strelok47311 ай бұрын
I think you lost a lot of people at 15:00 when you cut and pasted some of the code. It wasn't exactly very visible what you were doing!
@skverskk6 жыл бұрын
if (!product) { return res.status(404).json({ message: 'Product Not Found' }); } The above code does not execute inside the orders.js file when you enter an invalid productId .Instead the .catch block executes where the code block sends a json(500). Anyone have a fix for this? Thanks in advance
@ManishSingh-bu4ps4 жыл бұрын
Someone please help me. I am always getting "quantity" as 1 no matter whatever I pass .
@babaentertainment18524 жыл бұрын
can anyone help me with this "Cannot read property 'ObjectId' of undefined"
@qaipak16 жыл бұрын
the logic at around 11:00 was very confusing. Can someone explain that to me?
@DieterWoestemeier6 жыл бұрын
That was required because he wanted to add more information to each record in the orders array, so he needs to loop through each order using the "map" and then adding the request element. This is not needed if you simply return the raw array.
@aleXelaMec6 жыл бұрын
why to use const and not var or let?!
@cbrudder846 жыл бұрын
Because you want to protect those from being re-declared. You can assign new values of course, but you wouldn't ever want to expose it globally and allow it to be a new data type or data structure. Var is deprecated and should really not be used. Rule of thumb, start with const until you have to use let, do not use var.
@joegibson9589 Жыл бұрын
You can avoid this error - Error [ERR_HTTP_HEADERS_SENT]: - by replacing the return statement with: throw 'Product not found';
@CarlosRuiz-uy7yi8 ай бұрын
But what causes the error?
@jumbo9996143 жыл бұрын
Anyone gets the same result as mine with .delete() method? If I try to delete the order that was already deleted, I get the result with "DeleteCount: 0". Mongoose kind of putting deleted item in the cache or something. So it remembers the deleted items. So if I delete the order with the same id multiple time, it doesn't show "invalid id". Also, in there is message in the console telling me that .remove is deprecated and suggest that .deleteOne, .deleteMany, and .bulkwrite should be used instead.
@saurabhgupta66913 жыл бұрын
How system will automatically detect product: productID in 5:16
@Telesky904 жыл бұрын
but patch?
@97DanielxD4 жыл бұрын
Hello! Does anyone know how can I solve the error : "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client" when I get products by their ID? Thanks a lot!
@97DanielxD4 жыл бұрын
@@joaobarbosa02 u were right! It was missing the "return" on my responses! Thank you so much!
@Zhango906 жыл бұрын
using Product.find() .exec() ....... gives me this error in postman = Product.find(...).exec(...).select is not a function" I don't really know what's the reason. if I remove exec it works. does Anyone know which kind of problem I'm facing?
@benjaminyang59406 жыл бұрын
i had the same issue, and i resolved it by placing .select() before .exec() give that a shot. hope it works!
@jahazielvazquez72644 жыл бұрын
at 15:45 I have a question (I'm super noob) Why do I need 2 promises. Or in other words, why do I need to .then() blocks. Can't I just store the order, the response, and the metadata, all in the same .then() block? I promise you, I'm not trying to be a wise guy. I really do want to know, because I'm not very good at this. If someone takes the time to respond, I'll be grateful :)
@minhhieple64835 жыл бұрын
I can understand mongoose.Promise = global.Promise; can you help me ?
@vm3504 жыл бұрын
16:44 this won't work, you will return the response to the next chain, which will try to set the header once more and throw an error, doing an if(promise){ } else { } branching instead and chaining the remaining execution that branches from a found product id onto the save() promise and letting the 404 close up the POST request instead solves this problem
@hemanthvarmas4 жыл бұрын
can you please elaborate on it, a bit more? I think this makes sense, but I can't fully understand it.
@hemanthvarmas4 жыл бұрын
In post man it is showing json "product not found" thing. But in the console I am getting a huge error. which is lines and lines of code that I can't paste here. Here's the look of it at the end. Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:526:11) at ServerResponse.header (E:\WebP\JSPractice odePractice estful-academind ode_modules\express\lib esponse.js:771:10) at ServerResponse.send (E:\WebP\JSPractice odePractice estful-academind ode_modules\express\lib esponse.js:170:12) at ServerResponse.json (E:\WebP\JSPractice odePractice estful-academind ode_modules\express\lib esponse.js:267:15) at E:\WebP\JSPractice odePractice estful-academind\api outes\orders.js:27:29 at processTicksAndRejections (internal/process/task_queues.js:97:5) { code: 'ERR_HTTP_HEADERS_SENT' } POST /orders 500 770.054 ms - 31 (node:24860) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:526:11) at ServerResponse.header (E:\WebP\JSPractice odePractice estful-academind ode_modules\express\lib esponse.js:771:10) at ServerResponse.send (E:\WebP\JSPractice odePractice estful-academind ode_modules\express\lib esponse.js:170:12) at ServerResponse.json (E:\WebP\JSPractice odePractice estful-academind ode_modules\express\lib esponse.js:267:15) at E:\WebP\JSPractice odePractice estful-academind\api outes\orders.js:42:29 at processTicksAndRejections (internal/process/task_queues.js:97:5) (node:24860) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:24860) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
@hemanthvarmas4 жыл бұрын
Thanks in advance!
@vm3504 жыл бұрын
@@hemanthvarmas The error printout you're getting seems large because its a stack trace, a lot of it coming from the express library source code. The most important part of the error, which will give you a clue about why the exception is being thrown is this: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client, and the line it refers to at the bottom of the stack trace here: E:\WebP\JSPractice odePractice estful-academind\api outes\orders.js:42:29. This error is node trying to tell you that you are trying to set something in the response header twice, likely this is happening because you are setting a code in the header up in the method, and then trying to reset it on a separate conditional branch after it has been returned.
@vm3504 жыл бұрын
The second error you're getting is trying to tell you that you are letting an exception thrown inside a promise go unhandled, you can check the promise that the stack trace is pointing at and see if you're missing a catch block for it. The exception that is being thrown and going unhandled is most likely the error above where you are trying to set a field in a header multiple times after returning it.
@udaipman77613 жыл бұрын
Does anyone did order.js in es6 method. I am continuously gett error validation error , product path required in postman
@udaipman77613 жыл бұрын
If any please reply
@Andrey-il8rh6 жыл бұрын
I still can't get the difference between a promise and "a real" promise. Could you explain in greater detail?
@TheNerdyDev6 жыл бұрын
Me too was wondering about the same thing...Please Max enlighten on this.
@academind6 жыл бұрын
Sorry for the confusion: I was not referring to different kinds of promises, just of different ways of getting them (directly chaining then()/catch() vs the need to use exec() first).
@Andrey-il8rh6 жыл бұрын
Academind yes, I got that, but what is really the difference? Are you getting different kinds of data when using exec() vs direct chaining?
@SalmenBejaoui6 жыл бұрын
Mongoose queries are not Promises. Queries do return a thenable, but if you need a real Promise you should use the exec method.
@Andrey-il8rh6 жыл бұрын
Is it? When we set Mongoose.Promise = global.Promise I assume Mongoose starts using global promise object, after all, if queries actually allow .then() as well as .catch() to be chained onto them what situation can actually require using .exec() in favor of not using it?
@yadavkishan37783 жыл бұрын
you can create same video mysql node js express same product(add,delete,edit,list),order(add,delete,edit,list), validation , join to table order and product and login ,registration authtoken routers all 14 video please created
@franzcarhuaricramarcelo66592 жыл бұрын
For the error: UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client. Try with async-await: router.post('/', async (req, res, next) => { try { const productFound = await Product.findById(req.body.productId).exec(); if (!productFound) { return res.status(404).json({ message: "product not found" }) } const creatredProduct = new Product({ // ... }); const documentCreated = await creatredProduct.save(); console.log('Create document', documentCreated); const response = { message: 'Created asset successfully', // ... }; res.status(201).json(response); } catch (err) { res.status(500).json({error: err.message}); } }); As displayed on the console, the error description is: UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()
@probablyme7121 Жыл бұрын
Thanks this worked for me
@ranielgarcia86855 жыл бұрын
Its kinda weird, when I pass req.params.orderId directly in Order.findById(), it cannot get the order details but when I assign the req.params.orderId value in id variable it works.. can someone tell me why?
@arvindjaiswal80136 жыл бұрын
kzbin.info/www/bejne/jHzYimuHqMiNrbs I don't know how to get the time-link in the comment so I copied the link at that duration. Please check the above link which should take you at 16:47 of the video. In that, you've put an IF condition thinking that if it passes, it won't go into the next then() method. But, it does. You do get the response as you've expected but try checking your console or put console.log() in the next then() method. You'll see it does go into that irrespective of the condition you've put. If you find it, please fix it with a good approach. I've done it somehow but I don't think I've made it the way it should be.
@jexxiewoo83905 жыл бұрын
Why this time don't need the new keyword to generate a new ObjectId? 4:53
@bomaspeedy23028 ай бұрын
Hello, did you find a solution yet? I get an error when trying to POST an order Here is the error { “error”: { “ message “ : “ Class constructor ObjectId cannot be invoked without ‘ new ‘ “ } }
@G1une6 жыл бұрын
By me POST with wrong product id doesn't work even without this check: if(!product) { return res.status(404).json({ message: 'Product not found' }); } I get this: { "message": "Product not found", "error": { "message": "Cast to ObjectId failed for value \"5a747be38f17ec1ea8255fd21\" at path \"_id\" for model \"Product\"", "name": "CastError", "stringValue": "\"5a747be38f17ec1ea8255fd21\"", "kind": "ObjectId", "value": "5a747be38f17ec1ea8255fd21", "path": "_id" } } What's the reason?
please do everything a litte bit slower, i am so lost after min. 15
@academind6 жыл бұрын
Sorry Luca, always hard to find the right balance between too fast and too slow. Did you try to change the playback speed in the KZbin player? Maybe this helps a bit here.
@Version6 жыл бұрын
I think it's difficult to follow because it doesn't show what shortcuts he's pressing. Also what he selected isn't what most people would think he is copying. So here's the breakdown, where the problem starts to happen for a lot of folks. If you go to your code before he starts moving anything before 15:00. You should've just typed inside .then() product => { ... and left it blank take everything after the first catch block and post it inside of the first .then(product => { ... }) then delete the catch(err => .. block because it will be redundant to the one you've just written for the .then(result => { .. order stored }