简体   繁体   中英

getting undefined error when i use populate in mongoose node js

    Hi all am getting undefined when want to use populate .with jobid which is unique in both collections.please anyone let me know what is the issue

    crewbiesJobsSchema.virtual('jobs', {
                ref: 'Jobs',
                localField: 'jobId', 
                foreignField: 'jobId',
                justOne: false,
            });
            CrewbiesJobs.find({}).populate('jobs').exec(function(error, CJobs) {
                console.log(CJobs,'jobs s')
            });

when i console am getting cJobs undefined .when i console am getting cJobs undefined .

** Crewbies Model**

let crewbiesJobsSchema = new mongoose.Schema({
    jobTitle: String,
    jobId: {type: Schema.Types.String, unique: true ,ref:'Jobs'},
    startDate: String,
    endDate: String,
    country: String
}, {timestamps: true, toJSON: { virtuals: true } });

**Jobs Model**

let jobsSchema = new mongoose.Schema({
    jobTitle: String,
    jobId: {type: Schema.Types.String, unique: true ,ref:'CrewbiesJobs'},
    jobDescription: String,
    postedDate: String,
    filter1: [
        {
            label: String,
            value: String
        }
    ],
    filter2: [
        {
            label: String,
            value: String
        }
    ],
    filter3: [
        {
            label: String,
            value: String
        }
    ]
}, {timestamps: true});

Hi all am getting undefined when want to use populate.with jobid which is unique in both collections.please anyone let me know what is the issue Hi all am getting undefined when want to use populate.with jobid which is unique in both collections.please anyone let me know what is the issue

Your ref is the reference to the name you supplied when you called mongoose.model(MODEL_NAME, schema) Here for your jobsSchema you told mongoose that this schema is called flash_jobs you need to reference this when you supply your ref in the virtual.

crewbiesJobsSchema.virtual("jobs", {
    ref: "flash_jobs", // MODEL_NAME
    localField: "jobId",
    foreignField: "jobId",
    justOne: false
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM