'(node:10544) Accessing non-existent property '...' of module exports inside circular dependency - MongoDB (Mongoose) - NodeJS (Express)
I'm building an API for a mobile app with mongoDB (mongoose 6.3.0) in NodeJS (Express 4.17.3).
The structure → Structure
In this API I have :
- users
- bubbles
One user can take part of mutiple bubbles and a bubble can have multiple users in it. A user has references to all the bubble_id that He takes part of. A bubble has references to all the user_id that take part in the bubble.
My user.model.js
username: {
type: String,
required: true,
},
local: {
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
},
bubbles: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Bubble',
required: false,
}],
}, {
versionKey: false,
});
My bubble.model.js
name: {
type: String,
required: true,
},
dateCreation: Date,
users: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
}],
userAdmin:{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
}
}, {
versionKey: false,
});
The thing is, I have created a method that add user to a bubble and updtate the "bubbles" key of my user, and i have the same for my bubble, I created a method that add bubble to a user and updtate the "users" key of my bubble.
My user.queries.js
const { addUserToBubble } = require("./bubbles.queries");
exports.addBubbleToUser = async (userId, body) => {
const bubbleId = body.bubbleId;
addUserToBubble(userId, bubbleId)
return User.findByIdAndUpdate(
userId, {
$push: {
bubbles: bubbleId,
},
}, {
new: true,
}
);
};
My bubbles.queries.js
const { addBubbleToUser } = require('./users.queries')
exports.addUserToBubble = async (bubbleId, body) => {
const userId = body.userId;
addBubbleToUser(userId, bubbleId)
return Bubble.findByIdAndUpdate(
bubbleId, {
$push: {
users: userId,
},
}, {
new: true,
}
);
};
I know there is something ronge in my structure but i can't figure it out. When I launch my server there is a Warning : Warning: Accessing non-existent property 'addUserToBubble' of module exports inside circular dependency
How can i solve my problem ? Thank you for your time :)
Ok little update i made something like that and it works :
//method to add a user to a bubble update the bubble collection and user collection
exports.addUserToBubble = async (userId, body) => {
const user = await User.findById(userId);
const bubble = await Bubble.findById(body.bubbleId);
const index = user.bubbles.indexOf(body.bubbleId);
if (index > -1) {
user.bubbles.splice(index, 1);
}
user.bubbles.push(body.bubbleId);
bubble.users.push(userId);
return Promise.all([
Bubble.findByIdAndUpdate(
body.bubbleId, {
$addToSet: {
users: bubble.users,
},
}, {
new: true,
}
),
User.findByIdAndUpdate(
userId, {
$addToSet: {
bubbles: user.bubbles,
},
}, {
new: true,
}
),
]);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
