继续使用Mongoose获取“超出了最大呼叫堆栈大小”

时间:2018-07-19 20:01:38

标签: javascript node.js mongodb express mongoose

我正在尝试添加好友的功能。该代码片段可以处理它。

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);
    },
  );
});

您可以通过 POST 收到两个ID。我正在Schema中寻找它们,然后将彼此推到朋友列表中。之后,我进行.save()res.send("success"); 一切似乎都还可以,但是我继续进入控制台:

/用户/ shchypylov /文档/项目/个人/社交/node_modules/mongoose/lib/utils.js:249 函数cloneObject(obj,options){                     ^ RangeError:超出最大调用堆栈大小     在cloneObject(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:249:21)     在克隆时(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16)     在cloneObject(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:257:11)     在克隆时(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16)     在cloneObject(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:257:11)     在克隆时(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16)     在cloneObject(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:257:11)     在克隆时(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16)     在cloneObject(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:257:11)     在克隆时(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16)     在cloneObject(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:257:11)     在克隆时(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16)     在cloneObject(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:257:11)     在克隆时(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16)     在cloneObject(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:257:11)     在克隆时(/Users/shchypylov/Documents/projects/personal/social/node_modules/mongoose/lib/utils.js:183:16) [nodemon]应用程序崩溃-等待文件更改,然后再开始...

我该如何解决?

0 个答案:

没有答案