ReferenceError:在初始化猫鼬之前无法访问“用户”

Posted

技术标签:

【中文标题】ReferenceError:在初始化猫鼬之前无法访问“用户”【英文标题】:ReferenceError: Cannot access 'User' before initialization mongoose 【发布时间】:2022-01-19 18:27:51 【问题描述】:

这个错误由​​猫鼬显示 在 order.model.js:6:52 初始化之前无法访问“用户”

但用户已经在那里

order.js

import mongoose from 'mongoose';
import Product from './product.model.js';
import User from './users.model.js';

const orderSchema = new mongoose.Schema(
    user:  type: mongoose.SchemaTypes.ObjectId, ref: User ,
    products: [ type: mongoose.SchemaTypes.ObjectId, ref: Product ],
    status:  type: String, required: true ,
);

const Order = mongoose.model('Order', orderSchema);

export default Order;

user.js

import mongoose from 'mongoose';
import Order from './order.model.js';

const userSchema = new mongoose.Schema(
    fname:  type: String, required: true ,
    lname:  type: String, required: true ,
    email:  type: String, required: true ,
    phone:  type: Number, required: true ,
    password:  type: String, required: true ,
    orders: [ type: mongoose.SchemaTypes.ObjectId, ref: Order ],
);

const User = mongoose.model('User', userSchema);

export default User;

【问题讨论】:

【参考方案1】:

不需要导入用户模型,试试下面的代码

user:  type: mongoose.SchemaTypes.ObjectId, ref: 'User' ,

【讨论】:

以上是关于ReferenceError:在初始化猫鼬之前无法访问“用户”的主要内容,如果未能解决你的问题,请参考以下文章

ReferenceError:在初始化之前无法访问“步骤”

TypeORM OneToMany 导致“ReferenceError:在初始化之前无法访问'<Entity>'”

未捕获的 ReferenceError:在初始化之前无法访问“GA”

const commandFolders = readdirSync('./commands'); ReferenceError:在 Object.<anonymous> 初始化之前无法访

ReferenceError:在初始化React Collapse Component之前无法访问词法声明'useStyles',axios获取数据材料ui useStyles

ReferenceError:TextEncoder 没有用猫鼬定义 Node.js