将猫鼬模式导入另一个模式文件使导入的模式未定义
import mongoose schema into another schema file makes the imported schema undefined
文件结构:
│ resolvers.js
│ schema.js
│
└───schemas
matchesSchema.js
playersSchema.js
teamsSchema.js
tournamentsSchema.js
所以我有 4 个模式,我想在我所有的模式中使用其他模式,但是当我导入它时出现错误:
C:\Users\phara0h\Dropbox\esports-scores.com\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.com\nodeTest\node_modules\mongoose\lib\schema.js:425:13)
at new Schema (C:\Users\phara0h\Dropbox\esports-scores.com\nodeTest\node_modules\mongoose\lib\schema.js:99:10)
at Object.<anonymous> (C:/Users/phara0h/Dropbox/esports-scores.com/nodeTest/src/schemas/tournamentsSchema.js:8:34)
at Module._compile (module.js:570:32)
at loader (C:\Users\phara0h\Dropbox\esports-scores.com\nodeTest\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (C:\Users\phara0h\Dropbox\esports-scores.com\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.com/nodeTest/src/schemas/teamsSchema.js:5:1)
at Module._compile (module.js:570:32)
at loader (C:\Users\phara0h\Dropbox\esports-scores.com\nodeTest\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (C:\Users\phara0h\Dropbox\esports-scores.com\nodeTest\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:487:32)
当我 console.log
导入变量时,它们是未定义的。
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 );
是的,我可以将它们全部放在一个文件中,但由于它们都相互使用,但在页面上,较低的方案不能包含在上述方案中。
感谢进阶
一切都很好,但似乎我们需要明确要求对象的模式部分。
而不是player: [PlayersSchema]
,
尝试player: [PlayersSchema.schema]
我的解决方案还有一个变通办法:
删除了其他模式的所有内容并使它们
Schema.Types.Mixed
最后我不需要这样做,因为我制作了一个 graphQL 服务器并且我意识到 GraphQL 以我想要的方式处理 output/input 而无需在 Mongoose 中定义它。
文件结构:
│ resolvers.js
│ schema.js
│
└───schemas
matchesSchema.js
playersSchema.js
teamsSchema.js
tournamentsSchema.js
所以我有 4 个模式,我想在我所有的模式中使用其他模式,但是当我导入它时出现错误:
C:\Users\phara0h\Dropbox\esports-scores.com\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.com\nodeTest\node_modules\mongoose\lib\schema.js:425:13)
at new Schema (C:\Users\phara0h\Dropbox\esports-scores.com\nodeTest\node_modules\mongoose\lib\schema.js:99:10)
at Object.<anonymous> (C:/Users/phara0h/Dropbox/esports-scores.com/nodeTest/src/schemas/tournamentsSchema.js:8:34)
at Module._compile (module.js:570:32)
at loader (C:\Users\phara0h\Dropbox\esports-scores.com\nodeTest\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (C:\Users\phara0h\Dropbox\esports-scores.com\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.com/nodeTest/src/schemas/teamsSchema.js:5:1)
at Module._compile (module.js:570:32)
at loader (C:\Users\phara0h\Dropbox\esports-scores.com\nodeTest\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (C:\Users\phara0h\Dropbox\esports-scores.com\nodeTest\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:487:32)
当我 console.log
导入变量时,它们是未定义的。
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 );
是的,我可以将它们全部放在一个文件中,但由于它们都相互使用,但在页面上,较低的方案不能包含在上述方案中。
感谢进阶
一切都很好,但似乎我们需要明确要求对象的模式部分。
而不是player: [PlayersSchema]
,
尝试player: [PlayersSchema.schema]
我的解决方案还有一个变通办法: 删除了其他模式的所有内容并使它们
Schema.Types.Mixed
最后我不需要这样做,因为我制作了一个 graphQL 服务器并且我意识到 GraphQL 以我想要的方式处理 output/input 而无需在 Mongoose 中定义它。