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

ExpressJS POST请求后未返回响应问题求助

Hey there, let's break down why your register_user function isn't sending a response back, and fix it step by step. Looking at your code snippet, there are a few critical gaps causing this issue:

Key Issues in Your Current Code

  • Unfinished response call: Your code cuts off at res.sendSt... — you never complete the response method (like res.send() or res.status().json()), so the request hangs waiting for a reply.
  • Missing async operation handling: If you're using DynamoDB (based on the TableName and Item structure), the put operation is asynchronous. You defined the params object but never executed the database write, and didn't wait for it to finish before sending a response.
  • No password confirmation check: You collected password2 but never verified it matches password — this is a critical validation step you're skipping.
  • No error handling: There's no logic to catch failures (like bcrypt errors or database write issues), which would also leave the request hanging if something goes wrong.

Fixed Code Example

Here's a revised version of your function with all these gaps addressed, using async/await for cleaner async handling:

// Make sure you've imported required modules first
const bcrypt = require('bcrypt');
const { DynamoDB } = require('aws-sdk');
const docClient = new DynamoDB.DocumentClient();

exports.register_user = async function(req, res) {
  try {
    // Destructure request body for cleaner code
    const { portalID, companyName, password, password2 } = req.body;

    // 1. Validate password match first
    if (password !== password2) {
      return res.status(400).json({ error: 'Passwords do not match' });
    }

    // 2. Add non-empty field checks (optional but recommended)
    if (!portalID || !companyName || !password) {
      return res.status(400).json({ error: 'All required fields must be filled' });
    }

    // 3. Hash the password
    const salt = bcrypt.genSaltSync(12);
    const hashedPassword = bcrypt.hashSync(password, salt);

    // 4. Define database params
    const params = {
      TableName: "HouseAccounts",
      Item: {
        "portalID": portalID,
        "companyName": companyName,
        "points": 0,
        "password": hashedPassword,
      }
    };

    // 5. Execute async database write and wait for it to finish
    await docClient.put(params).promise();

    // 6. Send successful response
    return res.status(201).json({ 
      message: 'User registered successfully', 
      user: { portalID, companyName, points: 0 } 
    });
  } catch (error) {
    // 7. Catch and handle any errors
    console.error('Registration failed:', error);
    return res.status(500).json({ error: 'Failed to register user. Please try again later.' });
  }
};

What Changed & Why

  • Async/await for database operations: Ensures we don't send a response until the user data is actually saved to the database.
  • Complete response calls: Every code path now returns a clear, structured JSON response with appropriate HTTP status codes (400 for bad requests, 201 for successful creation, 500 for server errors).
  • Validation checks: Added password match and required field checks to catch invalid requests early.
  • Error handling: The try/catch block catches any unexpected issues (like database connection errors) and ensures the client always gets a response instead of a hanging request.

内容的提问来源于stack exchange,提问作者Vincent Nguyen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:27:01