Пустой массив после подписки - PullRequest
0 голосов
/ 08 мая 2020

Я пытаюсь подписаться на массив продуктов, но результат - пустой массив.

Перед подпиской:

enter image description here

После подписки:

enter image description here

Что касается моего бэкэнда, все отлично работает на почтальоне и 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
            });
        });
    
    });    

По сути, моя основная цель - добиться этого:

enter image description here Сервис :

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

Приветствую!

...