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

Node+Express连接MongoDB:GET正常POST无响应问题排查

MongoDB POST更新接口套接字挂起问题

我正在对MongoDB中存储的用户数据执行更新操作,已按规范拆分文件目录,目标是更新用户数据中的字段并保存至MongoDB。目前GET方法可正常获取数据,但通过POST方法实现更新时出现套接字挂起,无任何响应。

以下是相关代码片段:

1. mongoDB.js

export const dbConnection = async (result) => {
    const client = new MongoClient(uri);
    await client.connect();
    const database = client.db(dbName);
    const collection = database.collection(collectionName);
    console.log('Connected to database');
    await collection.insertMany(result.data);
    return collection;
};

2. mongoDBService.js(辅助函数文件)

const getResults = async () => {
    const myPosts = await fetch('https://reqres.in/api/users?page=1');
    const results = await myPosts.json();
    return results;
};

const getResultById = async (id) => {
    const users = await getResults();
    return users.data.find(user => user.id === id);
};

export {
    getResultById,
    getResults,
};

3. app.js(GET方法正常,POST方法异常)

app.get('/users', async (req, res) => {
    try {
        const email = req.query.email;
        const first_name = req.query.first_name;
        const last_name = req.query.last_name;
        const id = req.query.id;
        const parsedId = parseInt(id);

        let users;

        if (email) {
            users = await getResultByEmail(email);
        } else if (first_name) {
            users = await getResultByFirstName(first_name);
        } else if (last_name) {
            users = await getResultByLastName(last_name);
        } else if (parsedId) {
            users = await getResultById(parsedId);
        }
        else {
            users = await getResults();
            await dbConnection(users);
        }

        res.send(users);
    } catch (error) {
        console.log(`ERROR: `, error.message);
        res.status(500).json({"error": error.message});
    }
});

app.post('/update/first_name', async (req, res) => {
    const id = req.query.id;
    const parsedId = parseInt(id);
    const user = await getResultById(parsedId);
    console.log(user);        // 可获取到用户数据
    console.log(req.body);   // 从此处开始无输出
    collection = await dbConnection(user);
    if (user) {
        const collection = await dbConnection(user);
        user.first_name = req.body.first_name;
        await collection.findOneAndUpdate(
            {
                id: parsedId
            },
            {
                $set: {
                    first_name: req.body.first_name
                }
            },
        );
        res.status(201).json({
            "success": true,
            "data": user
        });
    } else {
        res.status(404).json({
            "message": 'User not found!'
        });
    }
});

更新first_name时使用的req.body格式如下:

{
   "first_name": "Jacob"
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 22:05:26