【问题标题】:I Want to remove the video if free_preview: false in nodejs express如果 nodejs express 中的 free_preview: false ,我想删除视频
【发布时间】:2022-01-17 02:56:32
【问题描述】:

如果它的free_preview为真意味着视频数据必须预览给所有人,当free_preview为假时意味着它不会暴露视频数据。

但在这种情况下,如果 free_preview 为 false,则视频数据也会暴露。

我想让它预览视频,无论 free_preview 是否为真,否则视频不会曝光。

我该如何解决这个问题?

-----------------API 代码--------------------

export const courses = async (req, res) => {
              const all = await Course.find({ published: true})
                .populate("instructor", "_id name")
                .exec();
              res.json(all)
            };
 import { json } from "express";
    import mongoose from "mongoose";
    
    const { ObjectId } = mongoose.Schema;
    
    const lessonSchema = new mongoose.Schema(
      {
        title: {
          type: String,
          trim: true,
          minlength: 3,
          maxlength: 320,
          required: true,
        },
        slug: {
          type: String,
          lowercase: true,
        },
        content: {
          type: String,
          minlength: 200,
        },
        video: {},
        time:{
          type: Number,
          required: true,
          trim: true,
          min: 1,
          max: 5
        },
        free_preview: {
          type: Boolean,
          default: false,
        },
      },
      { timestamps: true }
    );
    
    
    const courseSchema = new mongoose.Schema(
      {
        name: {
          type: String,
          trim: true,
          minlength: 3,
          maxlength: 320,
          required: true,
        },
        slug: {
          type: String,
          lowercase: true,
        },
        description: {
          type: {},
          minlength: 200,
          required: true,
        },
        price: {
          type: Number,
          default: 500,
        },
        image: {},
        category: String,
        published: {
          type: Boolean,
          default: false,
        },
        paid: {
          type: Boolean,
          default: true,
        },
        instructor: {
          type: ObjectId,
          ref: "User",
          required: true,
        },
        Category:{
          type : String,
          trim: true,
          min: 4,
          max: 200
        },
        lessons: [lessonSchema],
      },
      { timestamps: true }
    );
    
    export default mongoose.model("Course", courseSchema);
----------------------------api response ----------------------------------
    [
        {
            "price": 400,
            "published": true,
            "paid": true,
            "_id": "61d73468f0ef1c2df856d580",
            "slug": "nmap-for-ethical-hacking",
            "instructor": {
                "_id": "61d73336f0ef1c2df856d57f",
                "name": "lenin royal"
            },
            "name": "Nmap for ethical hacking",
            "description": "sfdsfdf",
            "lessons": [
                {
                    "free_preview": false,
                    "_id": "61d73721da89b03378321bc3",
                    "title": "intro",
                    "content": "sdfdfdff",
                    "video": {
                        "Location": "https://berrys01.s3.ap-south-1.amazonaws.com/_k8b_Khyz9GgTZkomm7WS.mp4",
                        "Bucket": "berrys01",
                        "Key": "_k8b_Khyz9GgTZkomm7WS.mp4",
                        "ETag": "\"00bf0fea24f997ac205c16de2ca3f7fa-2\""
                    },
                    "slug": "intro",
                    "updatedAt": "2022-01-06T18:38:25.674Z",
                    "createdAt": "2022-01-06T18:38:25.674Z"
                },
                {
                    "free_preview": true,
                    "_id": "61d737b8da89b03378321bc4",
                    "title": "hgdsfhgsdf",
                    "content": "dfsdfdf",
                    "video": {
                        "Location": "https://berrys01.s3.ap-south-1.amazonaws.com/Nqc6YL5LG1r3kWZIPigc1.mp4",
                        "Bucket": "berrys01",
                        "Key": "Nqc6YL5LG1r3kWZIPigc1.mp4",
                        "ETag": "\"4fbfb64ae636171fa7a7020ecc3b6a9e-16\""
                    },
                    "slug": "hgdsfhgsdf",
                    "updatedAt": "2022-01-06T18:40:56.963Z",
                    "createdAt": "2022-01-06T18:40:56.963Z"
                },
                {
                    "free_preview": true,
                    "_id": "61d737cada89b03378321bc5",
                    "title": "hello",
                    "content": "dfgdfg",
                    "video": {
                        "Location": "https://berrys01.s3.ap-south-1.amazonaws.com/fv5jEXzphsJ9al9DMKAr3.mp4",
                        "Bucket": "berrys01",
                        "Key": "fv5jEXzphsJ9al9DMKAr3.mp4",
                        "ETag": "\"72e0076dec633d7f3630628e92ba1891-3\""
                    },
                    "slug": "hello",
                    "updatedAt": "2022-01-06T18:41:14.132Z",
                    "createdAt": "2022-01-06T18:41:14.132Z"
                },
                {
                    "free_preview": true,
                    "_id": "61d737d6da89b03378321bc6",
                    "title": "rgrg",
                    "content": "dgdf",
                    "video": {
                        "Location": "https://berrys01.s3.ap-south-1.amazonaws.com/gmYnDGSrspH4jgfhC8ybr.mp4",
                        "Bucket": "berrys01",
                        "Key": "gmYnDGSrspH4jgfhC8ybr.mp4",
                        "ETag": "\"72e0076dec633d7f3630628e92ba1891-3\""
                    },
                    "slug": "rgrg",
                    "updatedAt": "2022-01-06T18:41:26.610Z",
                    "createdAt": "2022-01-06T18:41:26.610Z"
                },
                {
                    "free_preview": true,
                    "_id": "61d737e2da89b03378321bc7",
                    "title": "rgdgry56t5",
                    "content": "5yr",
                    "video": {
                        "Location": "https://berrys01.s3.ap-south-1.amazonaws.com/hzRW7TJn9ichITOk-3A-w.mp4",
                        "Bucket": "berrys01",
                        "Key": "hzRW7TJn9ichITOk-3A-w.mp4",
                        "ETag": "\"72e0076dec633d7f3630628e92ba1891-3\""
                    },
                    "slug": "rgdgry56t5",
                    "updatedAt": "2022-01-06T18:41:38.985Z",
                    "createdAt": "2022-01-06T18:41:38.985Z"
                }
            ],

【问题讨论】:

    标签: node.js api express


    【解决方案1】:

    据我所知,解决方案是做这样的事情

    const courses = await Course
      .find({ published: true })
      .populate({
        path: 'lessons',
        match: { free_preview: true },
        select: 'name -_id'
      })
      .exec();
    

    【讨论】:

    • 感谢您的回复,但您的代码实际上不起作用,因为课程是一个对象,所以请提供替代解决方案。
    • 我认为你需要注册你的关系,而不是 mongoose.model("Lesson", courseSchema) const courseSchema = new mongoose.Schema({ ... Lesson: [{ type: Schema. Types.ObjectId, ref: 'Lesson' }] }) 如果您想将课程作为非对象,我认为您可能需要稍后过滤结果
    猜你喜欢
    • 2013-06-27
    • 1970-01-01
    • 2012-02-01
    • 2018-09-10
    • 1970-01-01
    • 2023-03-26
    • 2018-10-30
    • 1970-01-01
    • 2020-02-21
    相关资源
    最近更新 更多