如何将路由传递的值传入Prisma函数?登录流程栈溢出排查
The maximum call stack overflow error occurs because you’re passing the entire Express req object to your Prisma login function instead of extracting the email from the request body. Here’s how to fix the data flow:
Step 1: Correct the Express Route Handler (users.js)
Your current code sends the full request object to Prisma, which triggers recursive processing of the large, nested req structure. Instead, extract the email from req.body and pass only that string to Prisma:
router.post('/login', async function (req, res) { // Ensure both req and res are included as parameters const { e_mail } = req.body; // Extract email from the JSON request body // Critical: Add this to your main Express app file if missing (to parse JSON bodies) // app.use(express.json()); console.log("Log message of user: " + e_mail); const loggedIn = await Api.login(e_mail); res.status(loggedIn ? 200 : 400).send(); })
Step 2: Validate Input in PrismaApi.js (Optional but Recommended)
Add a quick check to ensure you’re receiving a valid string before querying the database:
async function login(e_mail) { if (!e_mail || typeof e_mail !== 'string') { console.error("Invalid email received:", e_mail); return false; } console.log("Log message of PrismaApi: " + e_mail); const loginCreds = await prisma.nutzer.findFirst({ where: { email: e_mail, }, }); return !!loginCreds; }
Step 3: Confirm Frontend Data Flow
Your frontend API (Api.ts) is correctly sending the e_mail field, so no changes are needed there. Once the email flow works, you can extend the logic to include password validation.
Root Cause of the Error
When you pass the entire req object to Prisma, it tries to traverse all nested properties of the request (headers, socket connections, etc.) recursively. This triggers infinite recursion in Prisma’s internal getDepth function, leading to the stack overflow.
内容的提问来源于stack exchange,提问作者GoethesRevenge

