Home > Back-end >  Vinculate Schemas on MongoDB using Express and mongoose
Vinculate Schemas on MongoDB using Express and mongoose

Time:04-25

I am trying to link my schemas within my "Movie" entity so that I can have my information both in the "Movie" entity and in the others, they are:

  • Category
  • Actor
  • director
  • Studio

for now I'm testing categories

My code is the following:

controllers/movie.js


const create = async (req, res) => {
    const content = req.body;

    const category = await Category.findById(content._id);
    const actor = await Actor.findById(content._id);
    const director = await Director.findById(content._id);
    const studio = await Studio.findById(content._id);
    
    const newMovie = new Movie({
        ...content,
        category,
        actor,
        director,
        studio
    });

    const savedMovie = await newMovie.save();

    category.movies = [...category.movies, savedMovie._id];
    await category.save();
    actor.movies = [...actor.movies, savedMovie._id];
    await actor.save();
    director.movies = [...director.movies, savedMovie._id];
    await director.save();
    studio.movies = [...studio.movies, savedMovie._id];
    await studio.save();

    res.status(201).json({
        message: 'Movie created successfully',
        movie: savedMovie
    });
};

models/movie.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const {ObjectId} = mongoose.Schema; 

const movieSchema = new Schema(
  {
    title: {
      type: String,
      required: true,
    },
    year: {
      type: Number,
      required: true,
    },
    duration: {
      type: String,
      required: true,
    },
    rating: {
      type: String,
      required: true,
    },
    score: {
      type: String,
      required: true,
    },
    category: {
      type: ObjectId,
      ref: "Category",
    },
    description: {
      type: String,
      required: true,
    },
    director: [{
      type: ObjectId,
      ref: "Director",
    }],
    actor: [{
      type: ObjectId,
      ref: "Actor",
    }],
    studio: {
      type: ObjectId,
      ref: "Studio",
    },
    poster: {
      type: String,
      required: true,
    },
    trailer: {
      type: String,
      required: true,
    },
  },
  {
    timestamps: true,
  }
);

module.exports = mongoose.model("Movie", movieSchema);

models/category.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const {ObjectId} = Schema;
const categorySchema = new mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
    },
    movies: [
      {
        type: ObjectId,
        ref: "Movie",
      }
    ]
  },
  {
    timestamps: true,
  }
);

module.exports = mongoose.model("Category", categorySchema);

controllers/category.js


const Category = require('../models/category');

const create = async (req, res) => {
    const category = Category(req.body);
    
    res.status(201).json(
        await category
        .save()
        .then(result => {
            res.status(201).json({
                message: 'Category created successfully',
                category: result
            });
        })
    );
};

Next I will show the error when making my request

Request

POST http://localhost:3000/api/v1/movies HTTP/1.1
Content-Type: application/json

{
        "title": "The Matrix",
        "year": 1999,
        "duration": "136 min",
        "rating": "R",
        "score": "8.7",
        "categoryId": "62650106b643152d5fc5204e",
        "description": "A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.",
        "directorId": ["626502e956cd00fe36692bf9"],
        "actorId": ["626501fc56cd00fe36692bf2"],
        "studioId": "626502ac56cd00fe36692bf7",
        "poster": "https://images-na.ssl-images-amazon.com/images/M/MV5BNzQzOTk3OTAtNDQ0Zi00ZTVkLWI0MTEtMDllZjNkYzNjNTc4L2ltYWdlXkEyXkFqcGdeQXVyNjU0OTQ0OTY@._V1_SX300.jpg",
        "trailer": "https://www.youtube.com/embed/m8e-FF8MsqU"
}

Response

TypeError: Cannot read property 'movies' of null at create (C:\Users\Ernesto\Desktop\streaming-backend\src\controllers\movie.js:26:36) at processTicksAndRejections (internal/process/task_queues.js:95:5)

CodePudding user response:

Actually the errors say it all.
Here is one of those "message": "Path 'trailer' is required."This means the movie doesn't have this attribute. Let's see why.

This is the code that you used to save a movie:

const newMovie = new Movie({
        content,
        category: category._id,
        actor: actor._id,
        director: director._id,
        studio: studio._id
    });             
    
    
    try {
        const result = await newMovie.save();

No sign of trailer, so the only place to look for is the content. You are assigning content = Movie(req.body) which eventually turns content into an object. So I will rewrite the data you pass to the constructor:

const content = {the properties in here}
const newMovie = new Movie({
        {the properties in here} //I replace the value of `content` here
    });

I hope you see the problem now. If not, reveal the answer below:

Use ...content in instead of just content

  • Related