使用 Express 和 mongoose 在 MongoDB 上使用 Vinculate Schemas
Vinculate Schemas on MongoDB using Express and mongoose
我正在尝试 link 我的“电影”实体中的模式,以便我可以在“电影”实体和其他实体中获得我的信息,它们是:
- 类别
- 演员
- 导演
- 工作室
现在我正在测试类别
我的代码如下:
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
});
})
);
};
接下来我将在提出请求时显示错误
请求
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"
}
回应
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)
其实错误说明了一切。
这是其中之一 "message": "Path 'trailer' is required."
这意味着该电影没有此属性。让我们看看为什么。
这是您用来保存电影的代码:
const newMovie = new Movie({
content,
category: category._id,
actor: actor._id,
director: director._id,
studio: studio._id
});
try {
const result = await newMovie.save();
没有 trailer
的迹象,所以唯一要寻找的地方是 content
。您正在分配 content = Movie(req.body)
最终将 content
变成一个对象。所以我会重写你传递给构造函数的数据:
const content = {the properties in here}
const newMovie = new Movie({
{the properties in here} //I replace the value of `content` here
});
我希望你现在看到问题了。如果不是,请在下面显示答案:
使用 ...content
而不是 content
我正在尝试 link 我的“电影”实体中的模式,以便我可以在“电影”实体和其他实体中获得我的信息,它们是:
- 类别
- 演员
- 导演
- 工作室
现在我正在测试类别
我的代码如下:
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
});
})
);
};
接下来我将在提出请求时显示错误
请求
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"
}
回应
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)
其实错误说明了一切。
这是其中之一 "message": "Path 'trailer' is required."
这意味着该电影没有此属性。让我们看看为什么。
这是您用来保存电影的代码:
const newMovie = new Movie({
content,
category: category._id,
actor: actor._id,
director: director._id,
studio: studio._id
});
try {
const result = await newMovie.save();
没有 trailer
的迹象,所以唯一要寻找的地方是 content
。您正在分配 content = Movie(req.body)
最终将 content
变成一个对象。所以我会重写你传递给构造函数的数据:
const content = {the properties in here}
const newMovie = new Movie({
{the properties in here} //I replace the value of `content` here
});
我希望你现在看到问题了。如果不是,请在下面显示答案:
使用
...content
而不是content