-2

Here is my schema

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const finalDocument = new Schema({
  name: {
    type: String,
  },
  Properties: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Properties",
  },
  borrower: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "borrowers",
  },
});

var FinalDocuments = mongoose.model("finalDocument", finalDocument);
module.exports = FinalDocuments;

and here are my routes.

finalDocument
    .find({})
    .populate("borrower")
    .exec()
    .then((brws) => {
      res.statusCode = 200;
      res.setHeader("Content-Type", "application/json");
      res.json(brws);
    })
    .catch((err) => {
      console.log("error in getting borrowers", err);
    });

and when I am calling this route, borrower is not populated. I am attaching the picture of Postman.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Can you also add other relevant schemas to the question? – NeNaD Oct 04 '22 at 21:34
  • The picture of your Postman application appears to be missing from the question. – halfer Oct 04 '22 at 21:38
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Oct 04 '22 at 21:38
  • Have you tried running this code independently of the Node application, to see if they work there? If you run a Mongoose/Mongo application on the console it is much easier to debug. – halfer Oct 04 '22 at 21:40
  • i haven't tried that, but this is my postman response: [ { "_id": "633c961cd9378702fa818925", "name": "hi", "__v": 0 } ] – ghulam muhammad Oct 05 '22 at 13:56
  • Please add your Postman response, in a code block, in the post itself (click the Edit link). Thanks! – halfer Oct 07 '22 at 14:16

1 Answers1

0

There seems no problem with your code.

Possible reasons are:

  1. The collection name you mentioned in the ref => borrower: { type: mongoose.Schema.Types.ObjectId, ref: "borrowers", } in your schema may be spelling mistake or defined wrong schema name
  2. May be borrowers collection doesn't have documents
  3. Update your mongoose version and check
Vivek Rahul
  • 314
  • 2
  • 15