汪汪一只猫
分析的问题,你的查询主体是课程Course,也就是通过CourseModal去查询,示例代码如下:CourseModal.findAll({attributes:['id','title','subtitle']});课程视频的数据CourseVideo,定义1对1的关系就好CourseModal.hasOne(CourseVideoModal,{as:'video',foreignKey:'course_id',sourceKey:'id'})//修改查询语句CourseModal.findAll({attributes:['id','title','subtitle'],include:[{model:CourseVideoModal,as:'video',attributes:['video_url']}]})课程订阅的数据,CourseSub;课程和他是1对多的关系,需要定义1对多,使用hasManyCourseModal.hasMany(CourseSubModal,{as:'courseSubList',foreignKey:'course_id',sourceKey:'id'})//修改查询语句CourseModal.findAll({attributes:['id','title','subtitle'],include:[{model:CourseVideoModal,as:'video',attributes:['video_url']},{model:CourseSubModal,as:'courseSubList',attributes:['status','user_id']},]})//最后出来的数据list应该是下面的类似的结构list=[{id,title,subtitle,video:{video_url},courseSubList:[{status,user_id}]}]//最后用js循环,处理下courseSubListlist.map(course=>{let{courseSubList=[]}=course;courseSubList=courseSubList.map(courseSub=>{return{status:courseSub.user_id==userId?'on':'off'}})course.courseSubList=courseSubList;returncourse;})不知道上面的实现算不算优雅,哈哈。