javascript - 订阅后的空数组
问题描述
我试图订阅一系列产品,但结果是一个空数组。
订阅前:
订阅后:
就我的后端而言,一切都在邮递员和 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 数组中
非常感激!
解决方案
推荐阅读
- r - 替换矩阵元素时 foreach 不起作用
- python - 对没有内置或导入函数的数字进行排序以对列表进行排序或计算平均值
- python - 权证的 Pip 安装失败
- netlogo - Netlogo & Levelspace:如何在两个子模型之间传递字符串?
- pyqt5 - PyQt5 主窗口不会在 Raspberry 3B+ 上呈现(Raspian Stretch,Python 3.5)
- sql - 列出可能每月更改的数据的最佳方法
- php - 从博客类别存档 url 中删除 /category/ slug
- sql - 修剪表列长度超过 x 个字符
- c++ - cpack 创建 zip 而不在 zip 中创建自命名目录
- ruby-on-rails - 如何使用 ruby 中的单行代码创建从 1 到 n 位的数组