Creating a virtual field using data from a find() method of another model in nestJs

Viewed 103

I'm facing the following issue.

I'm working inside a schema creating this virtual field in one collection but to fill this field i need to get the data from another collection.

I need to somehow inject the nestJs mongoose library and then use the .find({}) method.

The problem i'm facing is that i'm not able to load the model inside the schema and the "mongoose" library isnt working.

the code is the following.

    export const SongSchema = new Schema({
  _id: { type: String },
    title:{type:String,required:true},
    singer: { type: String, required: true },
    imageURL: { type: String},
    asset: { type: Schema.Types.ObjectId, ref: 'Asset' },
    list: { type: Schema.Types.ObjectId, ref: 'List' },
    createdAt:{
        type:Date,
        default:Date.now
    },
  deleted: { type: Boolean },
  status: { type: Boolean }
},
{toJSON:{
  virtuals:true}
})

var Point = mongoose.model('Points', PointSchema)
async function getTotals  (id) {
 var points = await Point.find({company:id}).catch((err)=>{console.log(err);
 })
  return points
  
} 

SongSchema.virtual('totalPoint').get(function () {
  var total = getTotals(this.id)
 })

Many thanks

0 Answers
Related