如何解决如何在单独的文件中链接两个猫鼬模式?
我正在创建一个基本的银行应用程序,并且在单独的文件中导入猫鼬模式存在问题。我有两个模式,一个用户和一个事务。我的用户架构中有一个名为transactionHistory的字段,该字段是事务对象的数组,我的事务架构中有发送者和接收者字段,这两个都是用户对象。
Transaction / model.js
[user]
email = myworkemail@gmail.com
User / model.js
git config --get-all user.email
当我尝试运行此代码时,它将引发错误
import mongoose from "mongoose";
import { UserSchema } from "./../User/model";
export const TransactionSchema = new mongoose.Schema(
{
sender: { type: UserSchema,required: true },receiver: { type: UserSchema,time: { type: Date,amount: { type: Number,},{ collection: "transactions" }
);
export const TransactionModel = new mongoose.model(
"Transaction",TransactionSchema
);
奇怪的是,当我将User模式复制到 Transaction / model.js 时,如下所示:
import mongoose from "mongoose";
import { TransactionSchema } from "./../Transaction/model";
export const UserSchema = new mongoose.Schema(
{
firstName: { type: String,lastName: { type: String,age: { type: Number,email: { type: String,password: { type: String,require: true },balance: { type: Number,transactionHistory: {
type: [TransactionSchema],default: undefined,required: true,{ collection: "users" }
);
export const UserModel = new mongoose.model("User",UserSchema);
一切正常。这使我相信我以某种方式导入了错误的模式。
解决方法
问题是两个文件中都有2个导出,在定义函数时只需要1个export remove export
关键字
const UserSchema = new mongoose.Schema() //<-- export keyword removed
export const UserModel = new mongoose.model("User",UserSchema); //<-- Exporting here
您需要修复两个文件。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。