You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB栈溢出异常:首次关联Outlet创建Remote时进程崩溃

Fixing Stack Overflow When Creating First Remote for Outlet on AWS

Hey there, let's break down why you're hitting this stack overflow issue only on AWS when creating the first Remote for an Outlet, and how to fix it.

Root Cause

The problem stems from a circular reference between your Remote and Outlet documents in memory. Here's what's happening:

  1. You retrieve the full Outlet document using Outlet.findById.
  2. You assign the entire outlet document to remoteData.outlet instead of just its ObjectId.
  3. After creating the Remote, you push the full remote document into outlet.remotes.
  4. Now you have a loop: the Remote references the full Outlet (which contains the Remote), and the Outlet references the full Remote (which contains the Outlet).
  5. When sending the response with res.json(remote), Mongoose's internal cloning/serialization process tries to recursively copy this circular structure, leading to infinite recursion and a stack overflow.

This only happens the first time because after the first Remote is added, subsequent creates don't trigger the same circular reference chain in the in-memory documents (or your local environment handles it more gracefully due to subtle version differences).

Solution

The fix is simple: use ObjectIds instead of full documents when setting up references. This breaks the circular loop and stops the infinite recursion.

Modified Backend Create Method

Here's how to adjust your create function:

create(req: any, res: any, next: any) {
    Outlet.findById(req.body.oId, function (err: any, outlet: any) {
        if (err) {
            return next(err);
        }
        var remoteData = req.body;
        delete remoteData.oId;
        // Use the ObjectId instead of the full outlet document
        remoteData.outlet = outlet._id; 
        Remote.create(remoteData, function (err: any, remote: any) {
            if (err) {
                return next(err);
            }
            // Push the Remote's ObjectId to the outlet's remotes array (explicit and safe)
            outlet.remotes.push(remote._id); 
            outlet.save(function (err: any) {
                res.json({
                    type: true,
                    data: remote,
                });
            });
        });
    });
}

Key Changes:

  1. remoteData.outlet = outlet._id: Ensures the Remote only stores a reference to the Outlet's ID, not the full document.
  2. outlet.remotes.push(remote._id): Explicitly pushes the Remote's ID to the Outlet's remotes array (Mongoose would handle converting the full document to an ID automatically, but this is safer and clearer).

Why This Works

By using ObjectIds instead of full documents for references, you eliminate the circular reference in memory. When Mongoose serializes the Remote document to JSON for the response, it only includes the Outlet's ID, not the full Outlet object—so there's no loop to cause infinite recursion.

This should resolve the stack overflow error on AWS while maintaining all your existing functionality.

内容的提问来源于stack exchange,提问作者Elia Weiss

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:52:25