在不同的父级上将子文档用作架构,将子文档用作具有相同架构的模型的问题

时间:2019-05-31 13:27:07

标签: mongoose

为什么我需要将我的架构“内容”用作“通道”架构和“玩家”架构的模型。

内容架构文件“ content.js”:

const mongoose = require('mongoose')
const Schema = mongoose.Schema

const Channel = require('./channel')
const User = require('./user')
const File = require('./file')

const ContentSchema = new Schema({
  title: {
    type: String,
    required: true
  },
  type: {
    type: String,
    required: true
  },
  contentData: {
    type: Schema.Types.Mixed,
    required: true
  },
  channel: { type: Schema.Types.ObjectId, ref: 'Channel' },
  user: { type: Schema.Types.ObjectId, ref: 'User' },
  file: { type: Schema.Types.ObjectId, ref: 'File' },
})

const Content = mongoose.model('Content', ContentSchema)

module.exports = Content

玩家模式“ player.js”:

const mongoose = require('mongoose')
const AutoIncrement = require('mongoose-sequence')(mongoose)
const Schema = mongoose.Schema

const Client = require('./client')
const Content = require('./content')

const PlayerSchema = new Schema({
  name: {
    type: String,
    required: true
  },
  playListHash: {
    type: String,
  },
  active: {
    type: Boolean
  },
  blockedContents: [{
    type: String
  }],
  client: { type: Schema.Types.ObjectId, ref: 'Client' },
  contents: [Content.schema],
}, { toJSON: { virtuals: true } })
PlayerSchema.plugin(AutoIncrement, {inc_field: 'playerId'})

const Player = mongoose.model('Player', PlayerSchema)

module.exports = Player

渠道架构“ channel.js”:

const mongoose = require('mongoose')
const AutoIncrement = require('mongoose-sequence')(mongoose)
const Schema = mongoose.Schema

const User = require('./user')
const Content = require('./content')
const Client = require('./client')

const ChannelSchema = new Schema({
  title: {
    type: String,
    required: true
  },
  user: { type: Schema.Types.ObjectId, ref: 'User' },
  client: { type: Schema.Types.ObjectId, ref: 'Client' },
  contents: [Content],
}, { toJSON: { virtuals: true } })
ChannelSchema.plugin(AutoIncrement, {inc_field: 'channelId'})

const Channel = mongoose.model('Channel', ChannelSchema)

module.exports = Channel

我需要在Player和Channel模式上使用相同的内容数组,但我需要用作模式,因为我使用_id进行控制,我用作子文档,而不用作引用,因为仅在channel或每个实​​例上使用内容播放器。

使用此代码,我没有收到任何错误,但是当我尝试更改要使用的通道架构(在channel.js上)时

  

内容:[Content.schema]

我收到此错误:

  

10:17:52 api.1 |   /home/will/versa/content-manager/node_modules/mongoose/lib/schema.js:407   api.1 | 10:17:52 |抛出新的TypeError('Invalid value for   模式数组路径' + prefix + key + ''); api.1 10:17:52 |
  ^ 10:17:52 api.1 | TypeError:架构数组路径的值无效   contents 10:17:52 api.1 |在Schema.add   (/home/will/versa/content-manager/node_modules/mongoose/lib/schema.js:407:13)   api.1 | 10:17:52 |在新的架构   (/home/will/versa/content-manager/node_modules/mongoose/lib/schema.js:117:10)   api.1 | 10:17:52 |在对象。   (/home/will/versa/content-manager/server/models/channel.js:9:23)   api.1 | 10:17:52 |在Module._compile(module.js:652:30)10:17:52   api.1 |在Object.Module._extensions..js(module.js:663:10)   api.1 | 10:17:52 |在Module.load(module.js:565:32)10:17:52   api.1 |在tryModuleLoad(module.js:505:12)10:17:52 api.1 |   在Function.Module._load(module.js:497:3)10:17:52 api.1 |在   Module.require(module.js:596:17)10:17:52 api.1 |按要求   (内部/module.js:11:18)10:17:52 api.1 |在   宾语。   (/home/will/versa/content-manager/server/models/content.js:4:17)   api.1 | 10:17:52 |在Module._compile(module.js:652:30)10:17:52   api.1 |在Object.Module._extensions..js(module.js:663:10)   api.1 | 10:17:52 |在Module.load(module.js:565:32)10:17:52   api.1 |在tryModuleLoad(module.js:505:12)10:17:52 api.1 |   在Function.Module._load(module.js:497:3)10:17:52 api.1 |   [nodemon]应用程序崩溃-等待文件更改,然后再开始...

当我尝试将Player模式(在player.js上)更改为使用时:

  

内容:[内容]

我收到此错误:

  

10:21:20 api.1 |   /home/will/versa/content-manager/node_modules/mongoose/lib/schema.js:724   api.1 | 10:21:20 |抛出新的TypeError('无效的架构   配置:'+ 10:21:20 api.1 | ^ 10:21:20 api.1 |   TypeError:无效的架构配置:model不是有效的类型   在数组contents中。有关详细信息,请参见bit.ly/mongoose-schematypes。   有效模式类型的列表。 api.1 | 10:21:20 |在   Schema.interpretAsType   (/home/will/versa/content-manager/node_modules/mongoose/lib/schema.js:724:15)   api.1 | 10:21:20 |在Schema.path   (/home/will/versa/content-manager/node_modules/mongoose/lib/schema.js:566:27)   api.1 | 10:21:20 |在Schema.add   (/home/will/versa/content-manager/node_modules/mongoose/lib/schema.js:426:12)   api.1 | 10:21:20 |在新的架构   (/home/will/versa/content-manager/node_modules/mongoose/lib/schema.js:117:10)   api.1 | 10:21:20 |在对象。   (/home/will/versa/content-manager/server/models/player.js:8:22)   api.1 | 10:21:20 |在Module._compile(module.js:652:30)10:21:20   api.1 |在Object.Module._extensions..js(module.js:663:10)   api.1 | 10:21:20 |在Module.load(module.js:565:32)10:21:20   api.1 |在tryModuleLoad(module.js:505:12)10:21:20 api.1 |   在Function.Module._load(module.js:497:3)10:21:20 api.1 |在   Module.require(module.js:596:17)10:21:20 api.1 |按要求   (内部/module.js:11:18)10:21:20 api.1 |在   宾语。   (/home/will/versa/content-manager/server/models/client.js:5:16)   api.1 | 10:21:20 |在Module._compile(module.js:652:30)10:21:20   api.1 |在Object.Module._extensions..js(module.js:663:10)   api.1 | 10:21:20 |在Module.load(module.js:565:32)10:21:20   api.1 | [nodemon]应用程序崩溃-等待文件更改   开始...

我不明白这一点。 我的代码有什么问题?

0 个答案:

没有答案