Search code examples
javascriptmongodbmongoose-schemamongoose-populate

Bidirectional relationship mongoDB


I have been trying to follow the documentation to make a relationship between movies and categories (also among others that I will mention below, but starting with categories as an example).

Well, below I have the code of the parts of my code and the error response.

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/movie.js

const Movie = require('../models/movie');
const Category = require('../models/category');
const Actor = require('../models/actor');
const Director = require('../models/director');
const Studio = require('../models/studio');

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
    });
};

Now my post request


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

{
        "title": "The Matrixxx",
        "year": 1999,
        "duration": "136 min",
        "rating": "R",
        "score": "8.7",
        "category": "6265ba915a8064456ac6231b",
        "description": "A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.",
        "director": ["626502e956cd00fe36692bf9"],
        "actor": ["626501fc56cd00fe36692bf2"],
        "studio": "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\default\Desktop\streaming-backend\src\controllers\movie.js:26:36) at processTicksAndRejections (internal/process/task_queues.js:95:5)

Thanks for the answers :3


Solution

  • I think you forgot to use the necessary search fields. In theory, this code should look like this.

    const category = await Category.findById(content.category);
    const actor = await Actor.findById(content.actor[0]);
    const director = await Director.findById(content.director[0]);
    const studio = await Studio.findById(content.studio);