1

I have a model called Audio which contain a field called Quality. Quality could contain one or multiple Quality model

Music.add({
quality: { type: Types.Relationship, ref: 'Quality',initial: true,  many: true },

printing the quality content for a Music item after saving the model with the following code will show an array of ids of the different qualities available for the item.

Music.schema.post('save', function(Music) {
console.log(Music.quality)
}

Instead of getting the quality ids array I want to get the complete information of each quality model. Shall I do this manually by looping on the quality array and then use findOne() for each each quality id to construct the JSON or is there a simpler way? I started doing something like this but I am not sure that this is the right way of doing this:

Music.schema.post('save', function(music) {
var qualityList = [];
for(var i=0; i < music.quality.length ; i++){
const id = music.quality[i];
Quality.model.find().where('_id', id).exec(function(err, item) {
        representations.push(item)
});
console.log(representations);
});
smas
  • 11
  • 1
  • use `populate` after save. this answer is similar https://stackoverflow.com/questions/13525480/mongoose-populate-after-save – Saikat Hajra Mar 19 '18 at 18:07

0 Answers0