我有两个扩展Typegoose的类(Item和Player)
在Player类中,我得到了一个Ref<Item>[]数组
@arrayProp({itemsRef: Item})
items?: Ref<Item>[];在我的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时,它们的items不是公知的,而是一个Array of ObjectId。
async findAll(): Promise<Player[]> | null {
return await this.playerModel.find().exec();
} PS:我正在使用带有nestjs和netsjs-typegoose的Typegoose
发布于 2018-08-14 17:08:24
好的,这就是你如何从typegoose类中填充一个Ref item (使用: 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;
}https://stackoverflow.com/questions/51825036
复制相似问题