New to mongoose! I want to get all orders and populate each order with the product schema. However when I test the route no information comes through when I have the products type as ObjectId. How can I find all orders , populate the order with its products, then print it out?
Get All Orders route
const express = require('express');
const router = express.Router();
const {Order}=require('../models/order.js');
router.get('/', (req, res) => {
Order.find({})
.populate({path:'products', select:'name'})
.then((orders)=>{
res.json(orders);
})
.catch((err)=>{
res.json(err);
});
});
order.js
const mongoose = require('mongoose');
const Schema= mongoose.Schema;
const productSchema = new mongoose.Schema(
{
name: String,
price: Number,
category: String
},
{ timestamps: true }
);
const orderSchema = new mongoose.Schema(
{
date: {
type:Date,
default: Date.now},
customerName: String,
customerAddress: String,
creditCard: Number,
products:[{
type: Schema.Types.ObjectId, ref:'product'
}]
},
{ timestamps: true }
);
const Order = mongoose.model('Order', orderSchema);
const Product = mongoose.model('product', productSchema);
module.exports = {
Order:Order,
Product:Product}