[英]How to link two mongoose schemas in seperate files?
我正在创建一个基本的银行应用程序,但在单独的文件中导入 mongoose 模式时遇到问题。 我有两个模式,一个用户和一个事务。 我的用户模式有一个名为 transactionHistory 的字段,它是一个交易对象数组,我的交易模式有发送者和接收者字段,它们都是用户对象。
交易/model.js
import mongoose from "mongoose";
import { UserSchema } from "./../User/model";
export const TransactionSchema = new mongoose.Schema(
{
sender: { type: UserSchema, required: true },
receiver: { type: UserSchema, required: true },
time: { type: Date, required: true },
amount: { type: Number, required: true },
},
{ collection: "transactions" }
);
export const TransactionModel = new mongoose.model(
"Transaction",
TransactionSchema
);
用户/model.js
import mongoose from "mongoose";
import { TransactionSchema } from "./../Transaction/model";
export const UserSchema = new mongoose.Schema(
{
firstName: { type: String, required: true },
lastName: { type: String, required: true },
age: { type: Number, required: true },
email: { type: String, required: true },
password: { type: String, require: true },
balance: { type: Number, required: true },
transactionHistory: {
type: [TransactionSchema],
default: undefined,
required: true,
},
},
{ collection: "users" }
);
export const UserModel = new mongoose.model("User", UserSchema);
当我尝试运行此代码时,它会引发错误
throw new TypeError('Invalid value for schema path `' + fullPath +
^
TypeError: Invalid value for schema path `sender.type`, got value "undefined"
at Schema.add (/home/typicalfork/Documents/Projects/typical-bank/node_modules/mongoose/lib/schema.js:473:13)
at Schema.add (/home/typicalfork/Documents/Projects/typical-bank/node_modules/mongoose/lib/schema.js:507:12)
at new Schema (/home/typicalfork/Documents/Projects/typical-bank/node_modules/mongoose/lib/schema.js:127:10)
at Object.<anonymous> (/home/typicalfork/Documents/Projects/typical-bank/src/Transaction/model.js:4:34)
at Module._compile (node:internal/modules/cjs/loader:1083:30)
at Module._compile (/home/typicalfork/Documents/Projects/typical-bank/node_modules/pirates/lib/index.js:99:24)
at Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
at Object.newLoader [as .js] (/home/typicalfork/Documents/Projects/typical-bank/node_modules/pirates/lib/index.js:104:7)
at Module.load (node:internal/modules/cjs/loader:948:32)
at Function.Module._load (node:internal/modules/cjs/loader:789:14)
at Module.require (node:internal/modules/cjs/loader:972:19)
at require (node:internal/modules/cjs/helpers:88:18)
at Object.<anonymous> (/home/typicalfork/Documents/Projects/typical-bank/src/User/model.js:2:1)
at Module._compile (node:internal/modules/cjs/loader:1083:30)
at Module._compile (/home/typicalfork/Documents/Projects/typical-bank/node_modules/pirates/lib/index.js:99:24)
at Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
奇怪的是,当我像这样将 User 模式复制到Transaction/model.js 时:
import mongoose from "mongoose";
// import { UserSchema } from "./../User/model";
export const UserSchema = new mongoose.Schema(
{
firstName: { type: String, required: true },
lastName: { type: String, required: true },
age: { type: Number, required: true },
email: { type: String, required: true },
password: { type: String, require: true },
balance: { type: Number, required: true },
transactionHistory: {
type: [TransactionSchema],
default: undefined,
required: true,
},
},
{ collection: "users" }
);
export const TransactionSchema = new mongoose.Schema(
{
sender: { type: UserSchema, required: true },
receiver: { type: UserSchema, required: true },
time: { type: Date, required: true },
amount: { type: Number, required: true },
},
{ collection: "transactions" }
);
export const TransactionModel = new mongoose.model(
"Transaction",
TransactionSchema
);
一切都按预期工作。 这让我相信我以某种方式导入了错误的模式。
问题是您在两个文件中都有 2 个导出,您只需要 1 个导出即可在定义函数时删除export
关键字
const UserSchema = new mongoose.Schema() //<-- export keyword removed
export const UserModel = new mongoose.model("User", UserSchema); //<-- Exporting here
您需要修复这两个文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.