mongodb-js / mongoose-autopopulate

Always populate() certain fields in your mongoose schemas
Apache License 2.0
221 stars 36 forks source link

Auto populate from another nested schema and a refpath #76

Closed danhab99 closed 3 years ago

danhab99 commented 3 years ago

What I want to do is autocomplete each link subdocument in userSchema. linkSchema.profile is references another nested schema, but it doesn't populate. linkSchema.network populates from a different collection just fine. Is there something I've missed?

const profileSchema = new mongoose.Schema({
  displayName: String,
  picture: String,
  primary: {
    type: Boolean,
    default: false
  }
})

const linkSchema = new mongoose.Schema({
  profile: {
    type: mongoose.Schema.Types.ObjectId,
    refpath: 'profiles',
    autopopulate: true
  },
  network: { 
    type: mongoose.Schema.Types.ObjectId, 
    ref: "Network",
    autopopulate: true
  },
  uuid: String,
  status: {
    type: String,
    default: 'ok'
  }
})

const userSchema = new mongoose.Schema({
  username: {
    type: String,
    requried: true
  },
  profilePic: String,
  auth: {},
  profiles: [profileSchema],
  links: {
    type: [linkSchema],
    default: []
  }
})
vkarpov15 commented 3 years ago

refpath should be refPath. Does that fix your issue?

danhab99 commented 3 years ago

That fixed it

I'm gonna scream