1

This is my router JS where I get document by Mongoose. So the thing is i have these nested data like oem, category and subcategory which I cannot get the whole details. I have been trying since one week but nothing complies with me. Can someone please help me out?

router.get("/product/:id/:product_id", (req, res) => {


 ProReg.findById(req.params.id)
    .populate({
      path: "products",
      populate: [
        {
          path: "products.oem",
          model: "company",
          select: "companyname"
        },
        {
          path: "category",
          model: "productcategory",
          select: "category"
        },
        {
          path: "subcategory",
          model: "productsubcategory",
          select: "subcategory"
        }
      ]
    })
    .exec(function(err, products) {
      if (err) {
        res.json(err);
      } else {
        res.json(products);
      }
    });
});

This is the result that I am getting

{
"_id": "5cd37f4fcd79b01040124dbc",
"refno1": "klklklkl",
"refno2": "klklklkl",
"date": "2019-05-08T00:00:00.000Z",
"customer": "5c98bb0a42207b16d8fbd3cf",
"customertype": "5c7a1a1d4913fa08ac75c027",
"department": "5cbd67c709aeca1ea480157a",
"products": [],
"__v": 3
}

The details of the product is not coming only. Now how can i get this product data along with the details of oem, category, and subcategory?

Without Populate this is the result:

  {
    "_id": "5cd37f4fcd79b01040124dbc",
    "refno1": "klklklkl",
    "refno2": "klklklkl",
    "date": "2019-05-08T00:00:00.000Z",
    "customer": {
        "_id": "5c98bb0a42207b16d8fbd3cf",
        "customername": "Raghav"
    },
    "customertype": {
        "_id": "5c7a1a1d4913fa08ac75c027",
        "customertype": "Government "
    },
    "department": {
        "_id": "5cbd67c709aeca1ea480157a",
        "department": "Hardware"
    },
    "products": [
        {
            "warrantyfrom": "2019-05-09T00:00:00.000Z",
            "warrantyto": "2019-05-30T00:00:00.000Z",
            "oemwarrantyfrom": "2019-05-14T00:00:00.000Z",
            "oemwarrantyto": "2019-05-14T00:00:00.000Z",
            "_id": "5cd37f60cd79b01040124dbd",
            "oem": "5cb6e026042460131454cf45",
            "category": "5c960902e5cf3429e06beb6c",
            "subcategory": "5ca35fbed6e1430df88954a6",
            "modelno": "A123888",
            "serialno": "fdsfdsfs"
        },
        {
            "warrantyfrom": "2019-05-09T00:00:00.000Z",
            "warrantyto": "2019-05-30T00:00:00.000Z",
            "oemwarrantyfrom": "2019-05-14T00:00:00.000Z",
            "oemwarrantyto": "2019-05-14T00:00:00.000Z",
            "_id": "5cd37f65cd79b01040124dbe",
            "oem": "5cb6e026042460131454cf45",
            "category": "5c960902e5cf3429e06beb6c",
            "subcategory": "5ca35fbed6e1430df88954a6",
            "modelno": "A123888",
            "serialno": "eeeeee"
        },
        {
            "warrantyfrom": "2019-05-09T00:00:00.000Z",
            "warrantyto": "2019-05-30T00:00:00.000Z",
            "oemwarrantyfrom": "2019-05-14T00:00:00.000Z",
            "oemwarrantyto": "2019-05-14T00:00:00.000Z",
            "_id": "5cd37f6fcd79b01040124dbf",
            "oem": "5c986a1e9b6bc614b8a551b9",
            "category": "5c960902e5cf3429e06beb6c",
            "subcategory": "5ca35fbed6e1430df88954a6",
            "modelno": "QW123",
            "serialno": "hhhhhh"
        }
    ],

1 Answers1

1

Did you try simply doing this?

ProReg.findById(req.params.id)
    .populate('products.oem')
    .populate('products.category')
    .populate('products.subcategory')
    .exec(function(err, products) {
      if (err) {
        res.json(err);
      } else {
        res.json(products);
      }
    });
});

Check out these related tickets:

Mongoose populate within an object?

Mongoose populate with array of objects containing ref

BenSower
  • 1,532
  • 1
  • 13
  • 26
  • Thankyou very much I just change the model reference from its root and gave this code of yours and its done. This is how i did it. –  May 24 '19 at 05:17
  • For the model you can visit this https://github.com/mernBakor/serviceNice/blob/master/models/Entries/ProReg.js and for the routes you can visit this link https://github.com/mernBakor/serviceNice/blob/master/routes/api/entries/proReg.js –  May 24 '19 at 05:24