我延长了 2 个课程Typegoose
(Item
and Player
)
In the Player
我的班级有一个数组Ref<Item>[]
@arrayProp({itemsRef: Item})
items?: Ref<Item>[];
In my PlayersService
,我用这个方法来推动Item
:
async pushItem( itemPlayerDto: {playerId: string, item: Item}) : Promise<Player> {
let player = await this.findById(itemPlayerDto.playerId);
player.items.push(itemPlayerDto.item);
return await new this.playerModel(player).save();
}
但是当我检索时Players
, their items
没有被填充,只是一个Array of ObjectId
.
async findAll(): Promise<Player[]> | null {
return await this.playerModel.find().exec();
}
PS:我将 Typegoose 与 Nestjs 和 netsjs-typegoose 一起使用
好的,这就是如何从 typegoose 类填充 Ref 项(使用:populate 方法):
async findByName(playerName: string): Promise<Player> | null {
let player = await this.playerModel.findOne({'displayName': playerName}).exec();
let playerWithItems = await player.populate({
path: 'items',
model: 'Item'
}).execPopulate();
return playerWithItems;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)