0
const cons = await Contact.find(where);
const cons2 = await countContracts(cons);
res.send(cons2);

async function countContracts(cons) {
  for (let i = 0; i < cons.length; i++) {
    cons[i].noOfContracts = await Contract.countDocuments({
      contactId: cons[i]._id,
    });
  }
  return cons;
}

I didn't get noOfContracts in the array of objects

Phil
  • 157,677
  • 23
  • 242
  • 245
NEET JASSI
  • 85
  • 1
  • 3
  • 12
  • 1
    Does this answer your question? [Convert Mongoose docs to json](https://stackoverflow.com/questions/9952649/convert-mongoose-docs-to-json). TL;DR use `Contact.find(where).lean()` to work with plain JS objects instead of Mongoose Documents – Phil Sep 28 '22 at 02:55
  • no I want to loop through mongoose documents – NEET JASSI Sep 28 '22 at 02:57
  • You're not understanding. Adding extra properties like `noOfContracts` onto documents won't be serialised to JSON when you send the response. If you want to add properties, work with plain objects – Phil Sep 28 '22 at 02:58

0 Answers0