2017-12-26 6 views
0

Ich habe ein Benutzerschema wie unten gezeigt, und ich versuche, die Live-Projekte-Array zu füllen, aber ich kann nicht herausfinden, wie Sie darauf zugreifen.Bestückung geschachtelten Array mit Mongoose

const userSchema = new Schema({ 
    local: { 
    email: String, 
    username: String, 
    password: String, 
    liveProjects: [{ 
     type: mongoose.Schema.Types.ObjectId, 
     ref: 'liveProject' 
    }] 
    }, 
    google: { 
    googleId: String, 
    email: String, 
    username: String, 
    liveProjects: [{ 
     type: mongoose.Schema.Types.ObjectId, 
     ref: 'liveProject' 
    }] 
    } 
}); 

const User = mongoose.model('user', userSchema); 
module.exports = User; 

Wenn es nicht eingebettet war ich nur

verwenden könnte
User.findById(id).populate('liveProjects').exec((err,projects)=>{}); 

Aber wie bekomme ich Zugang zu 'local.liveProjects' oder 'google.liveProjects', damit ich sie bevölkern können?

Antwort

0

Stellt sich heraus, es genauso einfach ist wie

User.findById(id).populate('local.liveProjects').exec((err,projects)=>{}); 
0

Benutzer Schema Hier

`let UserSchema = new Schema({ 
    email: { 
     type: String, 
     unique: true, 
     required: true 
    }, 
    password: { 
     type: String, 
     required: true 
    }, 
    profile: { 
     firstName: {type: String,required: true}, 
     lastName: {type: String,required: true}, 
     address1: {type: String}, 
     address2: {type: String}, 
     city: {type: String}, 
     state: {type: String}, 
     zip: {type: String}, 
     phone: {type: String,required: true}, 
     avatar:{ type: Schema.Types.ObjectId, ref: 'Avatar'}, 
     shippingAddress:{ 
      address1: {type: String}, 
      address2: {type: String}, 
      city: {type: String}, 
      state: {type: String}, 
      zip: {type: String} 
     }, 
    }, 
    redemptionCards : [{ type: Schema.Types.ObjectId, ref: 'CardCodes' }] 
});` 

Logic RedemptionCards zu erhalten: -

User.findOne({ email }).populate("redemptionCards") 
      .exec(function (err, user) { 
       CardData.populate(user.redemptionCards, {path: 'redemptionCards',match: { _id: { $ne: null }}}, function (err, cards) { 
    console.log(cards); 
}); 

FYI - CardData das Fest programmierte JSON-Datei. Hoffe das hilft.

+0

redemptionCards im Schema nicht aufgeführt ist? –

+0

Ja ist es. Hier gehts los: redemptionCards: [{type: Schema.Types.ObjectId, ref: 'CardCodes'}], –

+0

Vielleicht vermisse ich etwas. In dem von Ihnen geposteten Schema erscheint replicationCards nicht als primäres Objekt oder Unterobjekt. –