0

I'm doing an update operation for desired user in users data which is stored in MongoDB. I have organised different files in different folders accordingly. I need to update a field in user data and store that in MongoDB.

I tried to do it in POST method, not getting any response because of socket hanging. But, I got data in GET method. I will provide some functions snippet here.

  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 (which contains helper functions):

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,
};
  1. app.js (GET method is working fine, but not 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);        // Getting user data by ID
    console.log(req.body);   // Not getting from here onwards
    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!'
        });
    }
});

Also, the req.body format I used to update first_name is as below:

{
   "first_name": "Jacob"
}
Geshode
  • 3,600
  • 6
  • 18
  • 32

0 Answers0