Home > Software engineering >  How to store all the data coming from a modal and not only it's ID ANGULAR 12
How to store all the data coming from a modal and not only it's ID ANGULAR 12

Time:03-10

I'll give you an example of what I exactly want to say because I couldn't find a better way to ask the question.

I have these interfaces :

import { Product } from "./product.model";
import { User } from "./user.model";

export interface Order{
  _id?: string;
  user: User;
  product: Product;
  quantity: number;
  price: number;
}


export interface Product {
  _id: string;
  title: string;
  price: number;
  images: string;
  stock: number;
  description: string;
  category: string;
  createdAt:Date
}

export interface User {
  _id:string,
  username:string,
  email:string,
  role:string,
  password:string,
  token:string
}

in the checkout component I have this function to create an order and store it :

createOrder(): void{
  this.cart.forEach((item)=>{
    this.order = {
      user: this.user,
      product: item.product, 
      price: item.product.price * item.number,
      quantity: item.number
    }

    this.orderService.post(this.order).subscribe(
      (res:Order) => {
        console.log('Order created');
      },
      (err)=>{
        console.log(err);
      })
  })
}

So everything is going okay the data is adedd successfully but It only store The PRODUCT ID and the USER ID not all the data of both of them.

I want to have the following:

{
  _id:1,
  user: {
    _id:2,
    username:'someone'
  },
  product: {
   _id: 1,
   name:'something',
   price: 99.99,
   stock: 244,
  },
  price: 201.00,
  quantity:2
}

but I only find:

 {
    _id:1,
    user:2,
    product:1,
    price: 201.00,
    quantity:2
 }

Any help would be appreciated.

CodePudding user response:

in mongoDB, there's an operator called populate that uses the id (user_id, product_id, order_id) and return all/part of its related data (user, product, order) based on their modals in the backend and depending on what parameters you choose to get back.

if you calling for orders list, with use of this operator you get user as an object of all user related data. the same for product.

So I think you need to check with your backend team, I think every backend technology has its own populate() operator or equivalent to it.

  • Related