I am working with 2 models in my project
import {model, Schema, Types} from 'mongoose'
interface IResource {
user : Types.ObjectId | IUsers,
type : Types.ObjectId | IResourceData,
value : number,
lastUpdate : number | Date,
const ResourceSchema = new Schema<IResource>({
user : {type : Types.ObjectId, ref : 'users'},
type : {type: Types.ObjectId , ref : 'resource_datas'},
lastUpdate : {type : Date , default : Date.now},
value : {type : Number, default : 500}
})
const Resources = model<IResource>('resources' , ResourceSchema)
interface IResourceData {
name : string,
}
const ResourceDataSchema = new Schema<IResourceData>({
name : {type : String},
})
const ResourceDatas = model<IResourceData>('resource_datas' , ResourceDataSchema)
However, when I try to find a Resource and populate its type, I encounter an issue accessing the type's name property
const userResource = await Resources.findOne({user : _id}).populate('type')
const resourceName = userResource.type.name //Error here
VSCode is displaying an error message that says:
Property 'name' does not exist on type 'ObjectId | IResourceData'.
Property 'name' does not exist on type 'ObjectId'.
What steps should I take to resolve this problem?