双向关系 mongoDB

Bidirectional relationship mongoDB

我一直在尝试按照文档在 moviescategories 之间建立关系(还有我将在下面提到的其他内容) ,但以类别为例)。

嗯,下面是我的部分代码和错误响应的代码。

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

现在我的 post 请求


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"
}

回应

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)

感谢您的回答:3

我想你忘了使用必要的搜索字段。理论上,这段代码应该是这样的。

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