简体   繁体   中英

How to link two mongoose schemas in seperate files?

I'm creating a basic bank app and I'm having issues with import mongoose schemas in separate files. I have two schemas, a user and a transaction. My user schema has a field called transactionHistory, which is an array of transaction objects, and my transaction schema has sender and receiver fields, which both are user objects.

Transaction/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
);

User/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);

When I try and run this code it throws the error

      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)

The weird thing is that when I copy the User schema to Transaction/model.js like this:

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
);

everything works as expected. This leads me to believe that I'm somehow importing the schemas wrong.

Issue is that you have 2 exports in both the files, you only need 1 export remove export keyword on defining function

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

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

You need to fix both your files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM