I will provide an example to clarify my question as I am struggling to find a better way to phrase it.
Here are the interfaces I am working with :
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, there is a function for creating and storing orders :
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);
})
})
}
The data is being successfully added but only the PRODUCT ID and the USER ID are stored, not all the data associated with them.
I would like the output to be structured like this:
{
_id:1,
user: {
_id:2,
username:'someone'
},
product: {
_id: 1,
name:'something',
price: 99.99,
stock: 244,
},
price: 201.00,
quantity:2
}
However, currently, it looks like this:
{
_id:1,
user:2,
product:1,
price: 201.00,
quantity:2
}
Any assistance on achieving the desired structure would be greatly appreciated.