Nithin
Nithin

Reputation: 1477

Mongoose virtual field for nested field returning null

My Schema looks like this, where associated is an array of String which are not _id and i use the Virtual method to populate another field in anoher Schema

const DetailSchema = mongoose.Schema({
candidate:{
    .
    .
    associated: [String],
    .
    .
}
},  { toObject: { virtuals: true }, toJSON: { virtuals: true } });

My Virtual looks like

DetailSchema.virtual('associatedJobs', {
    ref: 'Jobs',
    localField: 'candidate.associated',
    foreignField: 'jobID',
    justOne: false
});

The returned field is always null. Is there something wrong?

Upvotes: 1

Views: 2864

Answers (2)

Rom
Rom

Reputation: 1838

Your reference to Jobs (ref: 'Jobs',), it maybe Job (ref: 'Job',) if you've declared your model is Job (without 's')

Your associatedJobs will be returned not in object, this is example, it maybe with format:

{
    "candidate": {
        "associated": [
            "J2",
            "J3",
            "J5"
        ]
    },
    "_id": "5c1b4ab6683beb0b8162c80f",
    "id": "D1",
    "__v": 0,
    "associatedJobs": [
        {
            "_id": "5c1b4ab6683beb0b8162c80b",
            "jobID": "J2",
            "name": "Job name 2",
            "__v": 0
        },
        {
            "_id": "5c1b4ab6683beb0b8162c80c",
            "jobID": "J3",
            "name": "Job name 3",
            "__v": 0
        },
        {
            "_id": "5c1b4ab6683beb0b8162c80e",
            "jobID": "J5",
            "name": "Job name 5",
            "__v": 0
        }
    ]
}

This is my solution for your problem, you can download on gist to run on local https://gist.github.com/huynhsamha/a728afc3f0010e49741ca627750585a0

My simple schemas as your schemas:

var DetailSchema = new Schema({
  id: String,
  candidate: {
    associated: [String]
  }
}, {
    toObject: { virtuals: true },
    toJSON: { virtuals: true }
});

var JobSchema = new Schema({
  jobID: String,
  name: String
});

DetailSchema.virtual('associatedJobs', {
  ref: 'Job',
  localField: 'candidate.associated',
  foreignField: 'jobID',
  justOne: false
});

var Detail = mongoose.model('Detail', DetailSchema);
var Job = mongoose.model('Job', JobSchema);

And you should add populate when find:

const d = await Detail.findOne({ id: 'D1' }).populate('associatedJobs'); 

Upvotes: 2

Mohammad Raheem
Mohammad Raheem

Reputation: 1157

You should mention both schema structure to find the error in your query ,I have post example of virtual field may be you get some help

 const mongoose = require('mongoose');
    mongoose.Promise = global.Promise;
    mongoose.connect('mongodb://localhost:27017/test', { useMongoClient: true });

    var PersonSchema = new mongoose.Schema({
      name: String,
      band: String
    });

    var BandSchema = new mongoose.Schema({
      name: String
    }, { toObject: { virtuals: true } });
    BandSchema.virtual('members', {
      ref: 'Person', // The model to use
      localField: 'name', // Find people where `localField`
      foreignField: 'band', // is equal to `foreignField`
      // If `justOne` is true, 'members' will be a single doc as opposed to
      // an array. `justOne` is false by default.
      justOne: false
    });

    var Person = mongoose.model('Person', PersonSchema);
    var Band = mongoose.model('Band', BandSchema);

Upvotes: 1

Related Questions