Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
Mongoose Populate when using ObjectId not working

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}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!