猫鼬填充嵌套路径

问题描述

我尝试填充嵌套路径失败。

这是我的架构-

const Game = new schema({    
    board: [{
        board: {
            type: String
        }
    }],length: {
        type: Number,min: 1,max: 9
    },players: [Player],isActive: {
        type: Boolean,default: true
    }
},{
    timestamps: true
});

玩家对象-

const Player = {
    user: {
        type: mongoose.Schema.Types.ObjectId,ref: 'User',required: true
    },score: {
        type: Number,min: 0        
    }
};

这是用户架构-

const User = new schema({
    username: {
        type: String,required: true,unique: true
    }
    age: number,type: {
        type: Number,default: 1
    },admin: {
        type: Boolean,default: false
    }
},{
    timestamps: true
});

这是我的填充-我试图从用户架构中获取用户名

const game = await Game.findOne({ _id: gameId })
.populate({ path: 'players',populate: {path: 'user',select: 'username'} })

这就是我得到的-

...
user: [Object]
...

我得到一个[Object]而不是对象的内容。

如果“游戏”模式下的“玩家”是单个对象,则可以正常工作;当转换为数组时,它无法工作

将感谢您的帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...