简体   繁体   English

订阅返回未定义的变量

[英]Subscribe returns undefined variable

So in general, the shopping cart details are stored in local storage, being parsed back and assinged to an array, and then should be subscribed and posted to MongoDB, but returns "undefined":所以一般来说,购物车详细信息存储在本地存储中,被解析回来并分配到一个数组中,然后应该订阅并发布到 MongoDB,但返回“未定义”:

checkout.ts结帐.ts

 confirmOrder(){ let orderDetails: any = {}; orderDetails.firstName = this.checkOutForm.controls['firstName'].value; orderDetails.lastName = this.checkOutForm.controls['lastName'].value; orderDetails.phone = this.checkOutForm.controls['phone'].value; orderDetails.address = this.checkOutForm.controls['address'].value; orderDetails.country = this.checkOutForm.controls['country'].value; orderDetails.city = this.checkOutForm.controls['city'].value; this.orderItem=[]; for (let i in this.productAddedToCart) { this.orderItem.push({ product_Name:this.productAddedToCart[i].product_Name, id:this.productAddedToCart[i].id, product_Price:this.productAddedToCart[i].product_Price, product_Quantity:this.productAddedToCart[i].product_Price, type:this.productAddedToCart[i].type, product_Img: this.productAddedToCart[i].product_Img, product_Description:this.productAddedToCart[i].product_Description, _id:this.productAddedToCart[i]._id }); } orderDetails.product= this.orderItem debugger console.log("orderDetails:", orderDetails.product) this.connectionService.sendReceipt(this.checkOutForm.value).subscribe((res) =>{ debugger console.log("res is:", res); }); this.orderService.CreateOrder(orderDetails).subscribe((data:OrderDetails) => { debugger console.log("data is:", data) // prints nothing debugger this.globalResponse = data console.log("data is:", this.globalResponse) //prints nothing debugger }); alert('Your order has been received.'); this.checkOutForm.reset(); this.disabledSubmitButton = true; this.router.navigate(['/']); localStorage.removeItem('product'); localStorage.removeItem('cartItemCount'); };

在此处输入图像描述

在此处输入图像描述 this.globalResonse is "undefined". this.globalResonse 是“未定义的”。 Why is it not possible to console.log(data)?为什么无法 console.log(data)? Doesnt it get subscribed?它不会被订阅吗?

order.service.ts: order.service.ts:

 CreateOrder(orderDetails:OrderDetails){ return this.http.post(`${this.uri}/order`, orderDetails,{ observe:'body'}) }

My backend if needed(works):如果需要,我的后端(有效):

 //create order orderRoute.route('/').post((req, res) =>{ Product.findById(req.body.productId).populate('product').then(product => { if(.product){ return res.status(404):json({ message; "product not found" }): } const order = new OrderDetails({ _id. new mongoose.Types,ObjectId(): product. req.body,productId: email. req.body,email: firstName.req.body,firstName: lastName. req.body,lastName: phone. req.body,phone: address. req.body,address; }). return order.save() }).then(result => { console;log(result). return res.status(200):json({ message, "Order was Created": order: { order_id. result,_id: product. result,product: firstName.result,firstName: lastName. result,lastName: email. result,email: phone. result,phone: address. result,address: createdAt, new Date, }: request:{ type,"GET": order_url: "http://localhost.5000/order/" + result;_id } }). }).catch(err => { console;log(err). res.status(500):json({ error;err }); }); });

I also receive 2 errors: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client我还收到 2 个错误:错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头

(node:17548) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client (节点:17548)UnhandledPromiseRejectionWarning:错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头

Much Appreciated!非常感激!

I think the reason for not to receive any data from executing CreateOrder() because it get an interesting HTTP error.我认为不从执行CreateOrder()接收任何数据的原因是因为它得到了一个有趣的HTTP错误。 From following this document I got this content and it says,从遵循这个文件我得到了这个内容,它说,

Error [ERR_HTTP_HEADERS_SENT] is an interesting error that is fired up when a server tries to send more than one response to a client.错误 [ERR_HTTP_HEADERS_SENT] 是一个有趣的错误,当服务器尝试向客户端发送多个响应时会触发该错误。 What this means is that for a given client request the server previously sent a response (either a success response with the resource requested or error response for a bad request) back to the client and now is unexpectedly trying to send another response这意味着对于给定的客户端请求,服务器先前向客户端发送了响应(请求资源的成功响应或错误请求的错误响应),现在意外地尝试发送另一个响应

So your backend tries to send the response twice.因此,您的后端会尝试发送两次响应。 that's the issue here.这就是这里的问题。 If you could open the network tab in the browser console you can inspect if it's true.如果您可以在浏览器控制台中打开网络选项卡,您可以检查它是否为真。
Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM