I am doing a tutorial course, and I am stuck on this part, I want to display the properties of the Product which is appearing in console.log but not on the web page. The confusing part is that console.log is showing the product correctly, but not the front-end.
This is the HTML front-end:
<div class="grid mb-5" *ngFor="let orderItem of order.orderItems">
<div class="col-2">{{ orderItem.product?.name }}</div>
<div class="col-2">{{ orderItem.product?.brand }}</div>
<div class="col-2">{{ orderItem.product?.category?.name }}</div>
<div class="col-2">{{ orderItem.product?.price | currency }}</div>
<div class="col-2">{{ orderItem.quantity }}</div>
</div>
Here is Order TS
/* eslint-disable @typescript-eslint/no-explicit-any */
import { User } from '@eshop-repository/users';
import { OrderItem } from './order-item';
export class Order {
id?: string;
orderItems?: OrderItem[];
shippingAddress1?: string;
shippingAddress2?: string;
city?: string;
zip?: string;
country?: string;
phone?: string;
status?: number;
totalPrice?: string;
user?: User;
dateOrdered?: string;
_id?: string;
}
Here is the code for Order Item TS:
/* eslint-disable @nrwl/nx/enforce-module-boundaries */
import { Product } from "@eshop-repository/products";
export class OrderItem {
product?: Product;
quantity?: number;
}
Model:
const mongoose = require ('mongoose');
const orderSchema = mongoose.Schema ({
orderItems: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'OrderItem',
required: true
}],
shippingAddress1: {
type: String,
required: true,
},
shippingAddress2: {
type: String,
},
city: {
type: String,
required: true,
},
zip: {
type: String,
required: true,
},
country: {
type: String,
},
phone: {
type: String,
required: true,
},
status: {
type: String,
required: true,
default: 'Pending',
},
totalPrice: {
type: Number,
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
dateOrdered: {
type: Date,
default: Date.now,
}
})
orderSchema.virtual('id').get(function() {
return this._id.toHexString();
});
orderSchema.set('toJSON', {
virtuals: true,
});
exports.Order = mongoose.model('Order', orderSchema);
Route:
router.get(`/:id`, async (req, res) => {
let order = await Order.findById(req.params.id)
.populate('user', 'name')
.populate('orderItems')
if(!order) {
res.status(500).json({success: false})
}
res.send (order);
})
Schemda of order-item:
const mongoose = require ('mongoose');
const orderItemSchema = mongoose.Schema ({
quantity: {
type: Number,
required: true
},
product: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Product'
}
})