Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda