Express routes not sending response Heroku-Postgres code H12

Viewed 15

I'm having an issue with my routes not sending responses to the frontend. I only have 3 routes so far, only two send responses, but neither are doing so. I am using node-postgres(pg). My register route seems to be working because when I register a user, it is reflected in the database. Here are the two routes in question.

// login 
app.post('/api/v1/login', checkInput, async (req, res, next) => {
    console.log(req.body)
    try {

        // find user 
        db.query(`SELECT * FROM users WHERE username = $1`, [req.body.username], async (err, user) => {
            if (err) throw err;

            // user not found
            if (!user) {
                res.send({message: 'error'});
            } else {

                // compare passwords
                const matchedPassword = await bcrypt.compare(req.body.password, user.password);

                // password doesn't match
                if (!matchedPassword) {
                    res.send({message: 'error'});
                } else {
                    // user found
                    req.session.user = user.username;
                    req.session.auth = true;
                    res.send({message: 'success'});
                }
            }
        })  

        
    } catch (error) {

        next(error);
    }

});

// register 
app.post('/api/v1/register', checkInput, async (req, res, next) => {
    console.log(req.body)
    try {

        // check if user already exists
        db.query(`SELECT username FROM users WHERE username = $1`, [req.body.username], (err, user) => {
            if (err || user) {
                res.send({message: 'error'});
            }
        });

        // user doesn't exist so create user
        // encrypt password
        const salt = await bcrypt.genSalt(3);
        const hashPassword = await bcrypt.hash(req.body.password, salt);
        db.query(`INSERT INTO users (username, password) VALUES ($1, $2)`, [req.body.username, hashPassword], (err, user) => {
            if (err) {
                res.send({message: 'error'});
            } else {
                res.send({message: 'success'});
            }
        });

    } catch (error) {

        next(error);

    }
});

Any help would be appreciated!

0 Answers
Related