admin管理员组文章数量:1300007
File structure:
│ resolvers.js
│ schema.js
│
└───schemas
matchesSchema.js
playersSchema.js
teamsSchema.js
tournamentsSchema.js
So I have 4 schema's and I want to use the other schema's in the all my schema's but when I import it I get an error:
C:\Users\phara0h\Dropbox\esports-scores\nodeTest\node_modules\mongoose\lib\schema.js:425
throw new TypeError('Invalid value for schema Array path `' + prefix + key + '`');
^
TypeError: Invalid value for schema Array path `matches`
at Schema.add (C:\Users\phara0h\Dropbox\esports-scores\nodeTest\node_modules\mongoose\lib\schema.js:425:13)
at new Schema (C:\Users\phara0h\Dropbox\esports-scores\nodeTest\node_modules\mongoose\lib\schema.js:99:10)
at Object.<anonymous> (C:/Users/phara0h/Dropbox/esports-scores/nodeTest/src/schemas/tournamentsSchema.js:8:34)
at Module._pile (module.js:570:32)
at loader (C:\Users\phara0h\Dropbox\esports-scores\nodeTest\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (C:\Users\phara0h\Dropbox\esports-scores\nodeTest\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:/Users/phara0h/Dropbox/esports-scores/nodeTest/src/schemas/teamsSchema.js:5:1)
at Module._pile (module.js:570:32)
at loader (C:\Users\phara0h\Dropbox\esports-scores\nodeTest\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (C:\Users\phara0h\Dropbox\esports-scores\nodeTest\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:487:32)
When I console.log
the imported variable they are undefined.
playersSchema.js:
import mongoose, { Schema } from 'mongoose';
import timestamps from 'mongoose-timestamp';
import { MatchesSchema } from './matchesSchema';
import { TeamsSchema } from './teamsSchema';
import { TournamentsSchema } from './tournamentsSchema';
// Mongoose Schema definition
export const PlayersSchema = new Schema({
active: Boolean,
captain: {type: Boolean, default: false},
activeTeam: String,
birthDate: Date,
country: String,
firstName: String,
lastName: String,
nickName: String,
matches: [MatchesSchema],
picture: String,
position: String,
steamId: String,
twitch: String,
teams: [TeamsSchema],
tournaments: [TournamentsSchema]
});
PlayersSchema.plugin(timestamps);
PlayersSchema.index({ activeTeam: 'text', country: 'text', firstName: 'text', lastName: 'text', nickName: 'text' });
export const PlayerDB = mongoose.model( 'Players', PlayersSchema );
File structure:
│ resolvers.js
│ schema.js
│
└───schemas
matchesSchema.js
playersSchema.js
teamsSchema.js
tournamentsSchema.js
So I have 4 schema's and I want to use the other schema's in the all my schema's but when I import it I get an error:
C:\Users\phara0h\Dropbox\esports-scores.\nodeTest\node_modules\mongoose\lib\schema.js:425
throw new TypeError('Invalid value for schema Array path `' + prefix + key + '`');
^
TypeError: Invalid value for schema Array path `matches`
at Schema.add (C:\Users\phara0h\Dropbox\esports-scores.\nodeTest\node_modules\mongoose\lib\schema.js:425:13)
at new Schema (C:\Users\phara0h\Dropbox\esports-scores.\nodeTest\node_modules\mongoose\lib\schema.js:99:10)
at Object.<anonymous> (C:/Users/phara0h/Dropbox/esports-scores./nodeTest/src/schemas/tournamentsSchema.js:8:34)
at Module._pile (module.js:570:32)
at loader (C:\Users\phara0h\Dropbox\esports-scores.\nodeTest\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (C:\Users\phara0h\Dropbox\esports-scores.\nodeTest\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:/Users/phara0h/Dropbox/esports-scores./nodeTest/src/schemas/teamsSchema.js:5:1)
at Module._pile (module.js:570:32)
at loader (C:\Users\phara0h\Dropbox\esports-scores.\nodeTest\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (C:\Users\phara0h\Dropbox\esports-scores.\nodeTest\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:487:32)
When I console.log
the imported variable they are undefined.
playersSchema.js:
import mongoose, { Schema } from 'mongoose';
import timestamps from 'mongoose-timestamp';
import { MatchesSchema } from './matchesSchema';
import { TeamsSchema } from './teamsSchema';
import { TournamentsSchema } from './tournamentsSchema';
// Mongoose Schema definition
export const PlayersSchema = new Schema({
active: Boolean,
captain: {type: Boolean, default: false},
activeTeam: String,
birthDate: Date,
country: String,
firstName: String,
lastName: String,
nickName: String,
matches: [MatchesSchema],
picture: String,
position: String,
steamId: String,
twitch: String,
teams: [TeamsSchema],
tournaments: [TournamentsSchema]
});
PlayersSchema.plugin(timestamps);
PlayersSchema.index({ activeTeam: 'text', country: 'text', firstName: 'text', lastName: 'text', nickName: 'text' });
export const PlayerDB = mongoose.model( 'Players', PlayersSchema );
matchesSchema.js:
import mongoose, { Schema } from 'mongoose';
import timestamps from 'mongoose-timestamp';
import { PlayersSchema } from './playersSchema';
import { TeamsSchema } from './teamsSchema';
import { TournamentsSchema } from './tournamentsSchema';
// Mongoose Schema definition
export const MatchesSchema = new Schema({
dateUTC: String,
ended: Boolean,
lenght: String,
matchDetails: Schema.Types.Mixed,
matchId: Number,
player: [PlayersSchema],
teams: [TeamsSchema],
tournament: {type: String, ref: TournamentsSchema },
winner: String
});
MatchesSchema.plugin(timestamps);
export const MatchesDB = mongoose.model( 'Matches', MatchesSchema );
teamsSchema.js
import mongoose, { Schema } from 'mongoose';
import timestamps from 'mongoose-timestamp';
import { PlayersSchema } from './playersSchema';
import { MatchesSchema } from './matchesSchema';
import { TournamentsSchema } from './tournamentsSchema';
// Mongoose Schema definition
export const TeamsSchema = new Schema({
country: String,
teamTag: String,
logo: String,
matches: [MatchesSchema],
name: String,
players: [PlayersSchema],
steamId: String,
url: String,
tournaments: [TournamentsSchema]
});
TeamsSchema.plugin(timestamps);
TeamsSchema.index({ teamTag: 'text', country: 'text', name: 'text' });
export const TeamsDB = mongoose.model( 'Teams', TeamsSchema );
tournamentsSchema.js
import mongoose, { Schema } from 'mongoose';
import timestamps from 'mongoose-timestamp';
import { PlayersSchema } from './playersSchema';
import { MatchesSchema } from './matchesSchema';
import { TeamsSchema } from './teamsSchema';
// Mongoose Schema definition
export const TournamentsSchema = new Schema({
description: String,
endDate: String,
itemdef: Number,
leagueid: Number,
matches: [MatchesSchema], //<--- this causes the error
name: String,
organizer: String,
production: String,
prizepool: String,
players: [PlayersSchema],
results: String,
startDate: String,
teams: [TeamsSchema],
tournamentUrl: String
});
TournamentsSchema.plugin(timestamps);
TournamentsSchema.index({ description: 'text', name: 'text', organizer : 'text' });
export const TournamentsDB = mongoose.model( 'Tournaments', TournamentsSchema );
Yes I could put them all in one file but since they all use each other but on the page lower schemes can't be included in to the above ones.
Thanx in advanced
Share Improve this question edited Apr 20, 2017 at 10:19 Phara0h asked Apr 19, 2017 at 17:54 Phara0hPhara0h 192 silver badges13 bronze badges 4-
It's checking in the wrong directory
nodeTest\node_modules\mongoose\lib\schema.js
, notice the 1st line of the error. I'm sure your schemas are in a different directory... Just change the import line from./playersSchema
to properly point to the correct directory/path – Searching Commented Apr 20, 2017 at 1:07 - @Searching added the file structure, as you can see they are all located in the same directory – Phara0h Commented Apr 20, 2017 at 10:20
-
So yup the import statement should be something like
/schemas/playerschema
, and similar. – Searching Commented Apr 20, 2017 at 20:20 -
@Searching I think you're misunderstanding me these are the files I got the issue with: they are all located in the same folder.
schema/matchesSchema.js
,schema/playersSchema.js
,schema/matchesSchema.js
,schema/teamsSchema.js
andschema/tournamentsSchema.js
So I want to include all the above mentioned files in each other, and they are all located in the same folder. – Phara0h Commented Apr 21, 2017 at 8:22
2 Answers
Reset to default 8All is good, but it seems that we need to explicitly ask for the schema part of the object.
Instead of player: [PlayersSchema]
,
try player: [PlayersSchema.schema]
My solution what is more a work around: removed all the inclusions of the other schema's and made them
Schema.Types.Mixed
In the end I didn't need to do this since I made a graphQL server and I realized that GraphQL handled the output/input in a manner I wanted without having to define it in Mongoose.
本文标签:
版权声明:本文标题:javascript - import mongoose schema into another schema file makes the imported schema undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741636369a2389662.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论