我在猫鼬节点js中使用填充时得到未定义的错误

时间:2019-10-21 06:58:27

标签: node.js express mongoose

    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});

当在两个集合中都使用唯一的popid .with jobid时,大家都变得不确定。请大家让我知道这是什么问题。 collections.please任何人让我知道是什么问题

1 个答案:

答案 0 :(得分:0)

您的引用是对您调用mongoose.model(MODEL_NAME, schema)时提供的名称的引用。在这里,您为jobsSchema所代表的猫鼬将此模式称为flash_jobs,在提供时需要引用此名称您的虚拟裁判。

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