Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

send response to client in node js express is not working

I am trying to send back response to the client but it seems it is not working, I don’t know why, because after all I am not getting any errors.

here is my code:

exports.login = function (req, res, next, con) {
    if (!req || !res || !next || !con) return res.status(500).set({ msg: 'null variables' });

    var email = req.body.email, password = req.body.password;
    if (!email || !password) return res.status(500).set({ msg: 'null values' });
    var format = /^[!#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]*$/;

    if (!email.match(format)) {
        query1 = "SELECT `id`, `password` FROM `ceo`.`users` WHERE `email`= '" + email + "'"
        // check users exists with this email
        con.query(query1, (err, result) => {
            const bcrypt = require('bcrypt');
            var jwtGenerator = require('./../../classes/JWT/JWTgenerator')

            if (err) return res.status(500).set({ msg: 'error checking' })
            if (result.length === 0) return res.status(500).set({ msg: 'wrong email' })
            // check password
            bcrypt.compare(password, result[0].password, function (err) {
                if (err) return res.status(500).set({ msg: 'wrong password' })

                // update refreh token in database
                const rtoken = jwtGenerator.generateRefreshToken({ id: result[0].id });
                var queryUpdate = "UPDATE `token` SET `_token` = '"+rtoken+"' WHERE `token`.`id` = '"+result[0].id+"';"
                con.query(queryUpdate, (err) => {
                    if (err) return res.status(500).set({ msg: 'error in update' })
                    res.status(200).set({
                        msg: {
                            accessToken: jwtGenerator.generateAccessToken({ id: result[0].id }),
                            refreshToken: rtoken
                        }
                    })
                });
            });

        });
    } else {
        return res.status(500).set({ msg: 'special chars' })
    }

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think you have to use .send instead of set !!

Like this :

res.status(500).send({ msg: 'null values' });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading