简体   繁体   中英

Node.js error invalid value for schema Array path

I can't figure out what is wrong with this code:

act.js

import mongoose from 'mongoose';
var Schema   = mongoose.Schema;

var ActSchema = mongoose.Schema(
  {
    name: { type: String },
    runs: [{ type: Schema.Types.ObjectId, ref: 'Run' }],
  }
)

export default mongoose.model('Act', ActSchema);

scheduleRun.js

import mongoose from 'mongoose';
import Act from './act'

var ScheduleRunSchema = mongoose.Schema(
  {
    act: [Act.ActSchema]
  }
)

export default mongoose.model('ScheduleRun', ScheduleRunSchema);

It gives the following error:

TypeError: Invalid value for schema Array path act

You are exporting a Mongoose model but trying to use the schema.

If the model has already been registered then you can get the schema like this:

var ActSchema = mongoose.model('Act').schema

Or you can try exporting your schema as a named export from act.js :

...

export var ActSchema = mongoose.Schema(
  {
    name: { type: String },
    runs: [{ type: Schema.Types.ObjectId, ref: 'Run' }],
  }
)

...

...and then import the schema to scheduleRun.js :

...

import { ActSchema } from './act'

var ScheduleRunSchema = mongoose.Schema(
  {
    act: [ActSchema]
  }
)

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM