我正在尝试订购产品数组,但结果是一个空数组。
订阅前:
订阅后:
就我的后端而言,一切都可以在邮递员和mongodb上完美运行
路由:
orderRoute.route('/').post((req, res) =>{
Product.find({_id: req.body._id})
// .select('product')
.populate('Product')
.then(product => {
if(!product){
return res.status(404).json({
message: "product not found"
});
}
const order = new OrderDetails({
_id: new mongoose.Types.ObjectId(),
orderItems:product,
// product_Quantity: req.body.product_Quantity,
// 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:result,
request:{
type:"GET",
order_url: "http://localhost:5000/order/" + result._id
}
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error:err.message
});
});
});
基本上,我的主要目标是实现这一目标:
CreateOrder(orderDetails:OrderDetails){
return this.http.post(`${this.uri}/order`, orderDetails)
}
OrderDetails.ts:
import OrderItem from './OrderItem';
import Product from './Product';
export default class OrderDetails {
firstName:String;
lastName: String;
email: String;
phone: Number;
address: String;
country:String;
city:String;
orderItems:[{type: Product, ref: 'Product', required: true}];
createdAt = new Date();
}
Checkout.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;
orderDetails.email = this.checkOutForm.controls['email'].value;
this.orderItems=[]
for (let i in this.productAddedToCart) {
this.orderItems.push({
product_Name: this.productAddedToCart[i].product_Name,
product_Quantity: this.productAddedToCart[i].product_Quantity,
product_Price: this.productAddedToCart[i].product_Price,
_id: this.productAddedToCart[i]._id,
type: this.productAddedToCart[i].type,
product_Description: this.productAddedToCart[i].product_Description,
id: this.productAddedToCart[i].id
});
debugger
}
orderDetails.orderItems = this.orderItems
debugger
console.log("orderDetails:", orderDetails)
this.orderService.CreateOrder(orderDetails).subscribe((data) => {
debugger
console.log("data is:", data)
debugger
this.globalResponse = data
console.log("globalResponse is:", this.globalResponse)
debugger
});
也许空数组的原因是因为产品_id没有通过服务发送到后端? 如果是这样,我该怎么做?由于产品_id存储在orderItem数组中
非常感谢!