Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在不同的文件中链接两个mongoose模式?_Javascript_Node.js_Mongodb_Mongoose_Mongoose Schema - Fatal编程技术网

Javascript 如何在不同的文件中链接两个mongoose模式?

Javascript 如何在不同的文件中链接两个mongoose模式?,javascript,node.js,mongodb,mongoose,mongoose-schema,Javascript,Node.js,Mongodb,Mongoose,Mongoose Schema,我正在创建一个基本的银行应用程序,但在单独的文件中导入mongoose模式时遇到问题。我有两个模式,一个用户和一个事务。我的用户模式有一个名为transactionHistory的字段,它是一个事务对象数组,我的事务模式有sender和receiver字段,这两个字段都是用户对象 Transaction/model.js 从“猫鼬”导入猫鼬; 从“/./User/model”导入{UserSchema}; export const TransactionSchema=new mongoose.S

我正在创建一个基本的银行应用程序,但在单独的文件中导入mongoose模式时遇到问题。我有两个模式,一个用户和一个事务。我的用户模式有一个名为transactionHistory的字段,它是一个事务对象数组,我的事务模式有sender和receiver字段,这两个字段都是用户对象

Transaction/model.js

从“猫鼬”导入猫鼬;
从“/./User/model”导入{UserSchema};
export const TransactionSchema=new mongoose.Schema(
{
发件人:{type:UserSchema,必需:true},
接收方:{type:UserSchema,必需:true},
时间:{type:Date,必需:true},
金额:{type:Number,必需:true},
},
{集合:“事务”}
);
export const TransactionModel=new mongoose.model(
“交易”,
交易模式
);
User/model.js

从“猫鼬”导入猫鼬;
从“/./事务/模型”导入{TransactionSchema};
export const UserSchema=new mongoose.Schema(
{
名字:{type:String,必需:true},
lastName:{type:String,必需:true},
年龄:{type:Number,必需:true},
电子邮件:{type:String,必需:true},
密码:{type:String,require:true},
余额:{type:Number,必需:true},
交易历史记录:{
类型:[TransactionSchema],
默认值:未定义,
要求:正确,
},
},
{集合:“用户”}
);
export const UserModel=new mongoose.model(“用户”,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)

一切正常。这让我相信我在某种程度上导入了错误的模式。

问题是两个文件中都有两个导出,在定义函数时只需要一个导出移除
export
关键字

const UserSchema = new mongoose.Schema() //<-- export keyword removed

export const UserModel = new mongoose.model("User", UserSchema); //<-- Exporting here
constuserschema=newmongoose.Schema()//