猫鼬模式,无效模式配置

时间:2020-10-06 09:44:17

标签: node.js mongodb mongoose

我有一个用户模式,其中包含电影数组 我创建了一个新的电影模式 但是我收到无效的架构配置错误。 我在做什么?

enter code here
const mongoose = require("mongoose");
const Movie = require("./movie-model");

const Schema = mongoose.Schema;

const User = new Schema({
  firstName: { type: String, required: true },
  lastName: { type: String, required: true },
  email: { type: String, required: true },
  password: { type: String, required: true },
  movies: { type: Movie },
});

module.exports = mongoose.model("user", User);
var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var Movie = new Schema(
  {
    name: { type: String, required: true },
    time: { type: String, required: true },
    rating: { type: Number, required: false },
    priorety: { type: Number, required: true },
  },
);

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

3 个答案:

答案 0 :(得分:0)

在电影模型中

module.export =电影

代替

module.exports = mongoose.model(“电影”,电影);

答案 1 :(得分:0)

用户有很多电影

const mongoose = require("mongoose");
const Movie = require("./movie-model");

const Schema = mongoose.Schema;

const User = new Schema({
  firstName: { type: String, required: true },
  lastName: { type: String, required: true },
  email: { type: String, required: true },
  password: { type: String, required: true },
  movies: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "movie",
    },
  ],
});

module.exports = mongoose.model("user", User);

我邀请您阅读有关One-to-Many Relationships with Embedded Documents

的更多信息

答案 2 :(得分:0)

您需要在用户集合中完成引用

movies: [
type: mongoose.Schema.ObjectId,
ref: 'user'
]
//instead of doing that
movies: { type: Movie },