-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pushing object to array of references and creating infinite loop #11756
Comments
Modify this script to demonstrate your issue. const mongoose = require("mongoose");
const {Schema} = mongoose;
const User = mongoose.model(
"User",
new Schema({
name: { type: String, required: true },
posts: [{ type: mongoose.Types.ObjectId, ref: "Post" }],
})
);
const Post = mongoose.model(
"Post",
new Schema({
creator: { type: Schema.Types.ObjectId, ref: "User" },
})
);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await User.create({
name: 'Test',
posts: []
});
const user = await User.findOne();
const post = new Post({ creator: user._id });
await post.save();
user.posts.push(post);
console.log(user)
await user.save();
}
run(); |
const mongoose = require("mongoose");
const { Schema } = mongoose;
const User = mongoose.model(
"User",
new Schema({
name: { type: String, required: true },
posts: [{ type: mongoose.Types.ObjectId, ref: "Post" }],
})
);
const Post = mongoose.model(
"Post",
new Schema({
creator: { type: Schema.Types.ObjectId, ref: "User" },
})
);
const getUser = async () => {
const user = await User.findOne();
return !user ? await User.create({ name: "Test", posts: [] }) : user;
};
async function run() {
await mongoose.connect("mongodb://localhost:27017");
await mongoose.connection.dropDatabase();
const user = await getUser();
const post = new Post({ creator: user });
await post.save();
user.posts.push(post);
console.log(user);
await user.save();
}
run(); Note that I use user object while creating post and not user._id. Also, while adding post to user.posts we push the whole post object and not post._id. This should create infinite loop, but we get an error only if we try to access the value of the user or the post. If we don't try to access their values, everything works fine and both gets added to the database (if user.posts already has one or more values we can even access the user or the post, and everything will work). |
hello what if i have an array of posts then what to do plz help |
@roshdiAlMajzoub please provide some code samples that demonstrate what you're trying to do. Seems like you're just looking for |
const skillsSchema =({ const Skill = mongoose.model('Skill', skillsSchema); const teacherSchema =({ }); const Teacher = mongoose.model('Teacher', teacherSchema); app.post('/taddcourse',function(req,res){
}); when I try to run the code this error appears |
@imengolea that's because |
Do you want to request a feature or report a bug?
bug
What is the current behavior?
The problem is that here we are creating post with the user object as a creator, and later we push the post object to user.posts. So, our post would look like this
and this would lead to a "Maximum call stack size exceeded" error, as expected. But this error happens only when user.posts array is empty and when we try to access the user or the post object after we push post to user.posts. If user.posts is not empty or if it is but we do not try to access the user or the post object "Maximum call stack size exceeded" error does not occur.
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
We should get an error every time our code runs and not just when user.posts array is empty and we try to access the user or the post object.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node 18.0.0 | Mongoose 6.3.2
The text was updated successfully, but these errors were encountered: