Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

76
Vistas
How can i create a module for a course website using mongoDB and node.js

/I want to create a module section for a course website for which I will need a lesson schema as well so How can I design lesson schema , module schema , and course schema so they work just how they are needed to workCurrently I am doing this/

import mongoose from 'mongoose'

const LessonSchema = new mongoose.Schema({
  title: String,
  content: String,
  resource_url: String
})

const ModuleSchema = new mongoose.Schema({
     name: {
       type: String, 
       required: true
     },
     lessons: [LessonSchema]
})

export const Module = mongoose.model('Module', ModuleSchema);

const CourseSchema = new mongoose.Schema({
  name: {
    type: String,
    trim: true,
    required: 'Name is required'
  },
  price: {
    type: String, 
    trim: true, 
    required: true
  }, 
  image: {
    data: Buffer,
    contentType: String
  },
  intro: {
    type: String, 
    required :true
  },
  description: {
    type: String,
    trim: true
  },
  category: {
    type: String,
    required: 'Category is required'
  },
  updated: Date,
  created: {
    type: Date,
    default: Date.now
  },
  instructor: {type: mongoose.Schema.ObjectId, ref: 'User'},
  published: {
    type: Boolean,
    default: false
  },
  modules: [ModuleSchema]
})

export default mongoose.model('Course', CourseSchema)

Above was the schema and this is logic

const newLesson = async (req, res) => {
  try {
    let lesson = req.body.lesson

    let course = await Course.find({modules: {_id: req.params.moduleId}})
    console.log(course)

  } catch (err) {
    return res.status(400).json({
      error: errorHandler.getErrorMessage(err)
    })
  }
}

const newModule = async (req, res) => {
  try {
    let lesson = req.body.lesson
    let result = await Course.findByIdAndUpdate(req.course._id, {$push: {modules: {name: req.body.name, lessons: lesson}}, updated: Date.now()}, {new: true})
                            .populate('instructor', '_id name')
                            .exec()
    res.json(result)
  } catch (err) {
    return res.status(400).json({
      error: errorHandler.getErrorMessage(err)
    })
  }
}

**I have been brainstorming this from a while and cant get through it do you know how can I shape the schema and logic so that I can push lessons in module and then module in course schema ? **

7 months ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos