Invalid Input for type integer when my input should be a uuid

37 Views Asked by At

I am using my api to talk to my database. My carpools table in my postgresql database contains a column: carpoolid with uuid underneath it. However, in my api, it keeps giving me this error:

error: invalid input syntax for type integer: "ddfd9fc4-554b-4840-8a7e-3efb555c90d5"

the string you are seeing should be my uuid but I do not know why it is saying invalid input syntax for type integer.

This is my route code:

app.post('/api/joinGroup/:carpoolid', async (req, res) => {
    const result = joinGroupSchema.validate(req.body);

    if (result.error) {
        return res.status(400).json({ error: result.error.details[0].message });
    }

    const carpoolID = req.params.carpoolid.toLowerCase() // No conversion here yet
    const firebaseUserID = req.body.firebase_user_id;

    console.log('Received carpoolID:', carpoolID);
    console.log('Received firebaseUserID:', firebaseUserID);

    try {
        console.log('Join Group Request Body:', req.body);

        console.log('Carpools:', carpools);

        // Check if carpoolID is null
        if (!carpoolID) {
            console.log('Invalid carpoolID:', carpoolID);
            return res.status(400).json({ error: 'Invalid carpoolID provided.' });
        }

        // Check if the group (carpool) exists
        const groupExists = carpools.some(c => c.carpoolID && c.carpoolID.toLowerCase() === carpoolID.toLowerCase());
        console.log('Group exists:', groupExists);
        if (!groupExists) {
            console.log('Group does not exist:', carpoolID);
            return res.status(404).json({ error: 'The group with the given ID was not found.' });
        }

        // Check if the user is already a member of the group
        const userExistsInGroup = await db.query('SELECT 1 FROM user_carpools WHERE firebase_user_id = $1 AND carpool_id = $2', [firebaseUserID, carpoolID.toLowerCase()]);
        console.log('User exists in group:', userExistsInGroup.rows.length > 0);
        if (userExistsInGroup.rows.length > 0) {
            console.log('User is already a member of the group:', firebaseUserID, carpoolID);
            return res.status(400).json({ error: 'User is already a member of the group.' });
        }

        // Add user to the group
        const addUserToGroupQuery = 'INSERT INTO user_carpools(firebase_user_id, carpool_id) VALUES ($1, $2)';
        await db.query(addUserToGroupQuery, [firebaseUserID, carpoolID]);

        console.log('User added to the group successfully:', firebaseUserID, carpoolID);
        return res.status(200).json({ message: 'User added to the group successfully' });
    } catch (err) {
        console.error(err);
        return res.status(500).json({ error: 'Internal Server Error' });
    }
});

This is my joingroup schema:

const joinGroupSchema = Joi.object({
    carpoolid: Joi.string().required(),
    firebase_user_id: Joi.string().required(),
});

The console.logs in the terminal show this for firebaseuserid and carpoolid:

Received carpoolID: DDFD9FC4-554B-4840-8A7E-3EFB555C90D5
Received firebaseUserID: 4wStZ584luP41EqIzrBAEkgV58i1

This means that it is recieving it properly from my swift code but is unable to convert it I think. However I do not know for sure so any help is much appreciated thank you very much!

0

There are 0 best solutions below