首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mongoose架构,架构配置无效

mongoose架构,架构配置无效
EN

Stack Overflow用户
提问于 2020-10-06 17:44:17
回答 3查看 41关注 0票数 1

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

代码语言:javascript
复制
enter code here
代码语言:javascript
复制
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);
代码语言:javascript
复制
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);
EN

回答 3

Stack Overflow用户

发布于 2020-10-06 17:50:22

在电影模型中

module.export =电影

而不是

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

票数 0
EN

Stack Overflow用户

发布于 2020-10-06 19:29:06

用户有很多电影

代码语言:javascript
复制
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的更多信息

票数 0
EN

Stack Overflow用户

发布于 2020-10-06 19:33:55

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

代码语言:javascript
复制
movies: [
type: mongoose.Schema.ObjectId,
ref: 'user'
]
//instead of doing that
movies: { type: Movie },
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64223287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档