N
N
Nikita Shchypylov2018-07-22 12:06:20
MongoDB
Nikita Shchypylov, 2018-07-22 12:06:20

RangeError: Maximum call stack size exceeded when saving multiple schemas in Mongoose?

Hello!
I am implementing a f-onal with the addition of a "friend" to the Mongoose schema. Here is the code on POST-route :

Router.route("/confirm_friend").post(function(req, res) {
  UserSchema.findOne({ _id: req.body.self }).then(
    (self) => {
      UserSchema.findOne({ _id: req.body.whom }).then(
        (whom) => {
          self.friends.push(whom)
          whom.friends.push(self);
          self.requests.splice(self.requests.indexOf(whom), 1);
          self.save();
          whom.save();
          res.send("success");
        },
        (err) => {
          res.send(err);
          console.log("--- err", err);
        },
      );
    },
    (err) => {
      console.log("--- err", err);
    },
  );
});

As you can see from the code, I get two id, self and whom , based on them I look for users in the database, remove the element from requests and add it to friends . Alas, I get this error:
function cloneObject(obj, options) {
^
RangeError: Maximum call stack size exceeded
at cloneObject (/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:249: 21)
at clone (/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16)
at cloneObject (/Users/shchypylov/Documents/projects/personal/social/node_modules/ mongoose/lib/utils.js:257:11)
at clone (/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16)
at cloneObject (/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/ lib/utils.js:257:11)
at clone (/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16)
at cloneObject (/Users/shchypylov/Documents/ projects/personal/social/node_modules/mongoose/lib/utils.js:257:11)
at clone (/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16)
at cloneObject (/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:257:11)
at clone (/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16)
at cloneObject (/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/ lib/utils.js:257:11)
at clone (/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16)
at cloneObject (/Users/shchypylov/Documents/ projects/personal/social/node_modules/mongoose/lib/utils.js:257:11)
at clone (/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16)
at cloneObject (/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:257:11)
at clone (/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16)

How to fix this?
Thanks

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question