Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

301
Views
ReferenceError: Cannot access 'User' before initialization mongoose

this error is shown by mongoose Cannot access 'User' before initialization at order.model.js:6:52

but User is already there

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;

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No need to import User model, try using following code

user: { type: mongoose.SchemaTypes.ObjectId, ref: 'User' },
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!