如你所见,我有两个模型 邮政

import mongoose from 'mongoose';

const PostSchema = new mongoose.Schema(
  {
    title: {
      type: String,
      required: true,
    },
    text: {
      type: String,
      required: true,
      unique: true,
    },
    tags: {
      type: Array,
      default: [],
    },
    viewsCount: {
      type: Number,
      default: 0,
    },
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: '用户',
      required: true,
    },
    imageUrl: String,
    comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],
  },
  {
    timestamps: true,
  },
);

export default mongoose.model('Post', PostSchema);

用户

import exp from "constants";
import mongoose from "mongoose";

const 用户Schema = new mongoose.Schema({
    fullName: {
        type: String,
        required:true,
    },
    email: {
        type: String,
        required:true,
        unique: true,
    },
    passwordHash: {
        type: String,
        required: true,
    },
    role: {
        type: String,
        enum: ["student", "instructor"],
        required: true,
      },
    avatarUrl: String,
},
{
    timestamps: true,
});

用户Schema.methods.isStudent = function () {
    return this.role == "student";
  };

  用户Schema.methods.isIsntructor  = function () {
    return this.role == "instructor ";
  };
  
export default mongoose.model('用户', 用户Schema);

如你所见,我有两个角色,教师和学生.现在我可以得到所有的帖子

export const getAll = async(req, res) => {
    try{
        const posts = await PostModel.find().populate('user').exec();

        res.json(posts);
    } catch(err){
        console.log(err);
        res.status(500).json({
          message: 'Can not get post',
        });
    }
}

但我想让所有的帖子都是由导师创建的.我如何实现这一点?

我试着像这样做铁匠

export const getAllByTeacher = async(req, res) => {
  try {
    const posts = await PostModel.find({role: "instuctor"}).populate('user').exec();

    res.json(posts);
} catch (e) {
    console.log(e);
    res.status(500).json({
        message: 'Can not get post'
    });
}
}

但是我不知道如何从用户那里访问角色,希望你们能帮助我!我被这个问题困扰了好几天.

推荐答案

您需要首先获取具有讲师角色的用户,然后您可以使用该信息来获取由这些讲师创建的帖子列表.

像这样的事情会奏效的.

export const getAllByTeacher = async(req, res) => {
    try {
        const instructors = [];
        const users = await UserModel.find({ role: "instructor" });
        users.map(u => {
            instructors.push(u._id);
        });
        const posts = await PostModel.find({ user: {$in: instructors} }).populate('user').exec();
        
        res.json(posts);
    } catch (err) {
        console.log(err);
        res.status(500).json({
          message: 'Can not get post',
        });
    }   
}

Javascript相关问答推荐

JavaScript Date对象在UTC中设置为午夜时显示不正确的日期

在JavaScript中检索一些文本

foreach循环中的Typescript字符串索引

*ngFor和@代表输入decorator 和选角闭合

D3多线图显示1线而不是3线

当运行d3示例代码时,没有显示任何内容

成功完成Reducers后不更新状态

如何为我的astro页面中的相同组件自动创建不同的内容?

屏幕右侧屏障上的产卵点""

为什么按钮会随浮动属性一起移动?

使用ThreeJ渲染的形状具有抖动/模糊的边缘

使用getBorbingClientRect()更改绝对元素位置

Angular 形式,从DOM中删除不会删除指定索引处的内容,但会删除最后一项

传递方法VS泛型对象VS事件特定对象

AddEventListner,按键事件不工作

基于产品ID更新条带产品图像的JavaScript命中错误

使用可配置项目创建网格

Chrome上的印度时区名S有问题吗?

如何检测当前是否没有按下键盘上的键?

在对象的嵌套数组中添加两个属性