Am I doing something wrong here? I've been trying to display virtuals on one of my Schema which is Party but it doesnt show. Someone help I've been searching on the web but nothing worked.I want to display Candidates Documents on Part document but not sure what's happening thoughconst partySchema = mongoose.Schema({name: {type: String,required: [true, 'Please provide a party'],unique: [true, 'Party already exists. Please try another party name'],maxLength: [20, 'Party Name should be less than 20 characters'],minLength: [4, 'Party Name should be greater than 4 characters']},slogan:{type: String,required: [true, 'Please provide a slogan'],maxLength: [20, 'Party Name should be less than 20 characters'],minLength: [5, 'Party Name should be greater than 5 characters']},createdAt:{type: Date,default: Date.now()}},{toObject: { virtuals: true },toJSON: { virtuals: true },});partySchema.virtual('candidates', {localField: '_id',foreignField: 'party',ref: 'candidate',});const partyModel = mongoose.model('party', partySchema);module.exports = partyModel;---------------------------------------------------const candidateSchema = mongoose.Schema({firstName: {type: String,require: [true, 'Please provide a Firstname'],maxLength: [true, 'Firstname must be less than 15 characters'],minLength: [true, 'Firstname must be greater than 3 characters'],},lastName: {type: String,require: [true, 'Please provide a LastName'],maxLength: [true, 'LastName must be less than 15 characters'],minLength: [true, 'LastName must be greater than 3 characters'],},birthday:{type: Date,require: [true, 'Please provide a birthday'],},address:{type: String,require: [true, 'Please provide a Address'],maxLength: [true, 'Address must be less than 15 characters'],minLength: [true, 'Address must be greater than 3 characters'],},mobile:{type: String,require: [true, 'Please provide a Mobile Number'],maxLength: [true, 'Mobile Number must be less than 20 characters'],minLength: [true, 'Mobile Number must be greater than 3 characters'],},totalVotes: {default: 0,type: Number},email:{type: String,unique: [true, 'Email already exists. Please provide another one'],require: [true, 'Please provide a Mobile Number'],maxLength: [true, 'Mobile Number must be less than 20 characters'],minLength: [true, 'Mobile Number must be greater than 3 characters'],},party:{type: mongoose.Schema.ObjectId,ref: 'Party'}},{toJSON: { virtuals: true },toObject: { virtuals: true }});
Submitted September 23, 2020 at 10:04AM by HoorayForVajayjay
No comments:
Post a Comment